没有合适的资源?快使用搜索试试~ 我知道了~
首页C++专业指南:从入门到精通
C++专业指南:从入门到精通
需积分: 1 0 下载量 144 浏览量
更新于2024-07-18
收藏 4.65MB PDF 举报
"C++ Notes for Professionals 是一本600多页的专业书籍,旨在帮助新手快速入门C++编程。书中包含各种专业提示和技巧,涵盖了从基础到高级的主题,如Hello World、注释、编译过程、函数、预处理器、字面量、内联变量以及运算符优先级等。"
在C++编程中,了解和掌握这些基础知识至关重要。让我们深入探讨一下这些章节的内容:
**Chapter 1: Getting started with C++**
这一章是学习C++的基础,引导新手进入编程世界。它包含了以下部分:
1. **Section 1.1: Hello World** - 这是每个程序员的起点,展示了如何打印出经典的“Hello, World!”,以此介绍基本的语法和输出语句。
2. **Section 1.2: Comments** - 解释了如何在代码中添加注释,这对于代码的可读性和团队合作至关重要。
3. **Section 1.3: The standard C++ compilation process** - 描述了从源代码到可执行文件的编译步骤,包括预处理、编译、链接等。
4. **Section 1.4: Function** - 引入函数的概念,它是C++中代码复用的基本单元。
5. **Section 1.5: Visibility of function prototypes and declarations** - 阐述了函数原型和声明的重要性,以及它们在不同作用域内的可见性。
6. **Section 1.6: Preprocessor** - 讲解预处理器的使用,如宏定义和条件编译,这在编写可配置的代码时非常有用。
**Chapter 2: Literals**
本章涉及C++中的字面量,包括:
1. **Section 2.1: this** - 指向对象自身的指针,在类方法中使用,帮助理解对象内部状态。
2. **Section 2.2: Integer literal** - 介绍了整型字面量的使用,包括不同基数(十进制、八进制、十六进制)和类型修饰符(如`L`或`u`)。
3. **Section 2.3: true** 和 **Section 2.4: false** - 介绍布尔值的字面量,是逻辑运算的基础。
4. **Section 2.5: nullptr** - C++11引入的空指针常量,更安全地替代了旧的`NULL`。
**Chapter 3: Inline variables**
关于内联变量,C++17引入了这个特性,允许在类定义中定义静态数据成员,这有助于减少代码冗余和提高效率。
**Chapter 4: Operator precedence**
这一章讲解了C++中的运算符优先级,这是编写正确表达式的关键:
1. **Section 4.1: Logical && and || operators: short-circuit** - 逻辑与和或运算符的短路行为,强调了它们在条件表达式中的不同。
2. **Section 4.2: Unary Operators** - 单目运算符,如取反(-)、递增/递减(++)、地址(&)和解引用(*)。
3. **Section 4.3: Arithmetic operators** - 讲解算术运算符,如加法(+), 减法(-), 乘法(*), 除法(/)和模运算(%)。
4. **Section 4.4: Logical AND and OR opera** - 未完成的段落,可能继续讲述逻辑与和或运算符的优先级规则。
这本书的内容不仅限于上述部分,还可能涵盖更多主题,如类、对象、模板、异常处理、输入/输出流、文件操作、内存管理等。对于想要深入C++编程的新手来说,这是一份宝贵的资源。
Section 112.2: Observer pattern 563 ...............................................................................................................................
Section 112.3: Factory Pattern 566 ..................................................................................................................................
Section 112.4: Builder Pattern with Fluent API 566 .........................................................................................................
Chapter 113: Singleton Design Pattern 570 ..........................................................................................................
Section 113.1: Lazy Initialization 570 ................................................................................................................................
Section 113.2: Static deinitialization-safe singleton 571 ................................................................................................
Section 113.3: Thread-safe Singeton 571 ........................................................................................................................
Section 113.4: Subclasses 571 ..........................................................................................................................................
Chapter 114: User-Defined Literals 573 ..................................................................................................................
Section 114.1: Self-made user-defined literal for binary 573 ........................................................................................
Section 114.2: Standard user-defined literals for duration 573 ....................................................................................
Section 114.3: User-defined literals with long double values 574 .................................................................................
Section 114.4: Standard user-defined literals for strings 574 .......................................................................................
Section 114.5: Standard user-defined literals for complex 575 ....................................................................................
Chapter 115: Memory management 576 ................................................................................................................
Section 115.1: Free Storage (Heap, Dynamic Allocation ...) 576 ....................................................................................
Section 115.2: Placement new 577 ...................................................................................................................................
Section 115.3: Stack 578 ....................................................................................................................................................
Chapter 116: C++11 Memory Model 579 ....................................................................................................................
Section 116.1: Need for Memory Model 579 ....................................................................................................................
Section 116.2: Fence example 580 ...................................................................................................................................
Chapter 117: Scopes 582 .................................................................................................................................................
Section 117.1: Global variables 582 ..................................................................................................................................
Section 117.2: Simple block scope 582 ............................................................................................................................
Chapter 118: static_assert 584 ...................................................................................................................................
Section 118.1: static_assert 584 ........................................................................................................................................
Chapter 119: constexpr 585 ...........................................................................................................................................
Section 119.1: constexpr variables 585 ............................................................................................................................
Section 119.2: Static if statement 586 ..............................................................................................................................
Section 119.3: constexpr functions 587 ............................................................................................................................
Chapter 120: One Definition Rule (ODR) 589 .......................................................................................................
Section 120.1: ODR violation via overload resolution 589 .............................................................................................
Section 120.2: Multiply defined function 589 ..................................................................................................................
Section 120.3: Inline functions 590 ..................................................................................................................................
Chapter 121: Unspecified behavior 592 ...................................................................................................................
Section 121.1: Value of an out-of-range enum 592 ........................................................................................................
Section 121.2: Evaluation order of function arguments 592 .........................................................................................
Section 121.3: Result of some reinterpret_cast conversions 593 .................................................................................
Section 121.4: Space occupied by a reference 594 .......................................................................................................
Section 121.5: Moved-from state of most standard library classes 594 .....................................................................
Section 121.6: Result of some pointer comparisons 595 ...............................................................................................
Section 121.7: Static cast from bogus void* value 595 ..................................................................................................
Section 121.8: Order of initialization of globals across TU 595 ....................................................................................
Chapter 122: Argument Dependent Name Lookup 597 .................................................................................
Section 122.1: What functions are found 597 .................................................................................................................
Chapter 123: Attributes 598 ..........................................................................................................................................
Section 123.1: [[fallthrough]] 598 ....................................................................................................................................
Section 123.2: [[nodiscard]] 598 .....................................................................................................................................
Section 123.3: [[deprecated]] and [[deprecated("reason")]] 599 ..............................................................................
Section 123.4: [[maybe_unused]] 599 ...........................................................................................................................
Section 123.5: [[noreturn]] 600 ........................................................................................................................................
Chapter 124: Recursion in C++ 602 ...........................................................................................................................
Section 124.1: Using tail recursion and Fibonnaci-style recursion to solve the Fibonnaci sequence 602 ................
Section 124.2: Recursion with memoization 602 ............................................................................................................
Chapter 125: Arithmitic Metaprogramming 604 ................................................................................................
Section 125.1: Calculating power in O(log n) 604 ...........................................................................................................
Chapter 126: Callable Objects 606 ............................................................................................................................
Section 126.1: Function Pointers 606 ...............................................................................................................................
Section 126.2: Classes with operator() (Functors) 606 .................................................................................................
Chapter 127: Client server examples 608 ..............................................................................................................
Section 127.1: Hello TCP Client 608 ..................................................................................................................................
Section 127.2: Hello TCP Server 609 ...............................................................................................................................
Chapter 128: Const Correctness 613 ........................................................................................................................
Section 128.1: The Basics 613 ...........................................................................................................................................
Section 128.2: Const Correct Class Design 613 .............................................................................................................
Section 128.3: Const Correct Function Parameters 615 ...............................................................................................
Section 128.4: Const Correctness as Documentation 617 ............................................................................................
Chapter 129: Parameter packs 621 ..........................................................................................................................
Section 129.1: A template with a parameter pack 621 ..................................................................................................
Section 129.2: Expansion of a parameter pack 621 ......................................................................................................
Chapter 130: Build Systems 622 .................................................................................................................................
Section 130.1: Generating Build Environment with CMake 622 ....................................................................................
Section 130.2: Compiling with GNU make 623 ...............................................................................................................
Section 130.3: Building with SCons 625 ...........................................................................................................................
Section 130.4: Autotools (GNU) 625 ................................................................................................................................
Section 130.5: Ninja 626 ....................................................................................................................................................
Section 130.6: NMAKE (Microsoft Program Maintenance Utility) 626 .........................................................................
Chapter 131: Concurrency With OpenMP 627 .......................................................................................................
Section 131.1: OpenMP: Parallel Sections 627 .................................................................................................................
Section 131.2: OpenMP: Parallel Sections 627 .................................................................................................................
Section 131.3: OpenMP: Parallel For Loop 628 ................................................................................................................
Section 131.4: OpenMP: Parallel Gathering / Reduction 628 ........................................................................................
Chapter 132: Resource Management 630 ..............................................................................................................
Section 132.1: Resource Acquisition Is Initialization 630 ................................................................................................
Section 132.2: Mutexes & Thread Safety 631 .................................................................................................................
Chapter 133: Storage class specifiers 633 ............................................................................................................
Section 133.1: extern 633 ...................................................................................................................................................
Section 133.2: register 634 ...............................................................................................................................................
Section 133.3: static 634 ...................................................................................................................................................
Section 133.4: auto 635 .....................................................................................................................................................
Section 133.5: mutable 635 ..............................................................................................................................................
Chapter 134: Linkage specifications 637 ...............................................................................................................
Section 134.1: Signal handler for Unix-like operating system 637 ...............................................................................
Section 134.2: Making a C library header compatible with C++ 637 ...........................................................................
Chapter 135: Digit separators 639 ............................................................................................................................
Section 135.1: Digit Separator 639 ...................................................................................................................................
Chapter 136: C incompatibilities 640 ........................................................................................................................
Section 136.1: Reserved Keywords 640 ...........................................................................................................................
Section 136.2: Weakly typed pointers 640 .....................................................................................................................
Section 136.3: goto or switch 640 ....................................................................................................................................
Chapter 137: Side by Side Comparisons of classic C++ examples solved via C++ vs C++11
vs C++14 vs C++17 641 .....................................................................................................................................................
Section 137.1: Looping through a container 641 ............................................................................................................
Chapter 138: Compiling and Building 642 ..............................................................................................................
Section 138.1: Compiling with GCC 642 ...........................................................................................................................
Section 138.2: Compiling with Visual Studio (Graphical Interface) - Hello World 643 ...............................................
Section 138.3: Online Compilers 648 ...............................................................................................................................
Section 138.4: Compiling with Visual C++ (Command Line) 650 ..................................................................................
Section 138.5: Compiling with Clang 653 ........................................................................................................................
Section 138.6: The C++ compilation process 653 ...........................................................................................................
Section 138.7: Compiling with Code::Blocks (Graphical interface) 655 ........................................................................
Chapter 139: Common compile/linker errors (GCC) 658 ...............................................................................
Section 139.1: undefined reference to `***' 658 ................................................................................................................
Section 139.2: error: '***' was not declared in this scope 658 ........................................................................................
Section 139.3: fatal error: ***: No such file or directory 660 ...........................................................................................
Chapter 140: More undefined behaviors in C++ 661 ........................................................................................
Section 140.1: Referring to non-static members in initializer lists 661 .........................................................................
Chapter 141: Unit Testing in C++ 662 .......................................................................................................................
Section 141.1: Google Test 662 .........................................................................................................................................
Section 141.2: Catch 662 ...................................................................................................................................................
Chapter 142: C++ Debugging and Debug-prevention Tools & Techniques 664 .................................
Section 142.1: Static analysis 664 ....................................................................................................................................
Section 142.2: Segfault analysis with GDB 665 ..............................................................................................................
Section 142.3: Clean code 666 .........................................................................................................................................
Chapter 143: Optimization in C++ 668 .....................................................................................................................
Section 143.1: Introduction to performance 668 ............................................................................................................
Section 143.2: Empty Base Class Optimization 668 ......................................................................................................
Section 143.3: Optimizing by executing less code 669 ..................................................................................................
Section 143.4: Using ecient containers 670 .................................................................................................................
Section 143.5: Small Object Optimization 671 ................................................................................................................
Chapter 144: Optimization 673 ..................................................................................................................................
Section 144.1: Inline Expansion/Inlining 673 ...................................................................................................................
Section 144.2: Empty base optimization 673 .................................................................................................................
Chapter 145: Profiling 675 .............................................................................................................................................
Section 145.1: Profiling with gcc and gprof 675 .............................................................................................................
Section 145.2: Generating callgraph diagrams with gperf2dot 675 ............................................................................
Section 145.3: Profiling CPU Usage with gcc and Google Perf Tools 676 ...................................................................
Chapter 146: Refactoring Techniques 678 ...........................................................................................................
Section 146.1: Goto Cleanup 678 ......................................................................................................................................
Credits 679 ............................................................................................................................................................................
You may also like 686 ......................................................................................................................................................
C++ Notes for Professionals 1
About
Please feel free to share this PDF with anyone for free,
latest version of this book can be downloaded from:
http://GoalKicker.com/CPlusPlusBook
This C++ Notes for Professionals book is compiled from Stack Overflow
Documentation, the content is written by the beautiful people at Stack Overflow.
Text content is released under Creative Commons BY-SA, see credits at the end
of this book whom contributed to the various chapters. Images may be copyright
of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not
affiliated with official C++ group(s) or company(s) nor Stack Overflow. All
trademarks and registered trademarks are the property of their respective
company owners
The information presented in this book is not guaranteed to be correct nor
accurate, use at your own risk
Please send feedback and corrections to web@petercv.com
C++ Notes for Professionals 2
Chapter 1: Getting started with C++
Version Standard Release Date
C++98 ISO/IEC 14882:1998 1998-09-01
C++03 ISO/IEC 14882:2003 2003-10-16
C++11 ISO/IEC 14882:2011 2011-09-01
C++14 ISO/IEC 14882:2014 2014-12-15
C++17 TBD 2017-01-01
C++20 TBD 2020-01-01
Section 1.1: Hello World
This program prints Hello World! to the standard output stream:
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
}
See it live on Coliru.
Analysis
Let's examine each part of this code in detail:
#include <iostream> is a preprocessor directive that includes the content of the standard C++ header file
iostream.
iostream is a standard library header file that contains definitions of the standard input and output
streams. These definitions are included in the std namespace, explained below.
The standard input/output (I/O) streams provide ways for programs to get input from and output to an
external system -- usually the terminal.
int main() { ... } defines a new function named main. By convention, the main function is called upon
execution of the program. There must be only one main function in a C++ program, and it must always return
a number of the int type.
Here, the int is what is called the function's return type. The value returned by the main function is an exit
code.
By convention, a program exit code of
0
or EXIT_SUCCESS is interpreted as success by a system that executes
the program. Any other return code is associated with an error.
If no return statement is present, the main function (and thus, the program itself) returns
0
by default. In this
example, we don't need to explicitly write return
0
;.
All other functions, except those that return the void type, must explicitly return a value according to their
return type, or else must not return at all.
剩余703页未读,继续阅读
2018-06-24 上传
222 浏览量
2013-02-06 上传
2010-06-07 上传
2009-05-13 上传
2007-12-05 上传
2010-09-15 上传
2010-05-18 上传
2013-04-01 上传
whvip2010
- 粉丝: 0
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 高清艺术文字图标资源,PNG和ICO格式免费下载
- mui框架HTML5应用界面组件使用示例教程
- Vue.js开发利器:chrome-vue-devtools插件解析
- 掌握ElectronBrowserJS:打造跨平台电子应用
- 前端导师教程:构建与部署社交证明页面
- Java多线程与线程安全在断点续传中的实现
- 免Root一键卸载安卓预装应用教程
- 易语言实现高级表格滚动条完美控制技巧
- 超声波测距尺的源码实现
- 数据可视化与交互:构建易用的数据界面
- 实现Discourse外聘回复自动标记的简易插件
- 链表的头插法与尾插法实现及长度计算
- Playwright与Typescript及Mocha集成:自动化UI测试实践指南
- 128x128像素线性工具图标下载集合
- 易语言安装包程序增强版:智能导入与重复库过滤
- 利用AJAX与Spotify API在Google地图中探索世界音乐排行榜
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功