C++ Notes for Professionals

Programming · notes

Voir tous les documents en programmation

C++

Notes for ProfessionalsC++

Notes for Professionals

600+ pages

of professional hints and tricks

GoalKicker.com

Free Programming Books

Disclaimer

This is an unocial free book created for educational purposes and is

not aliated with ocial C++ group(s) or company(s).

All trademarks and registered trademarks are

the property of their respective owners

Contents

About

...................................................................................................................................................................................

1

Chapter 1: Getting started with C++

....................................................................................................................

2

Section 1.1: Hello World

Section 1.2: Comments

Section 1.3: The standard C++ compilation process

Section 1.4: Function

Section 1.5: Visibility of function prototypes and declarations

Section 1.6: Preprocessor

.................................................................................................................................................

..................................................................................................................................................

..................................................................................................

......................................................................................................................................................

..............................................................................................................................................

9

.................................................................................

8

Chapter 2: Literals

Section 2.1: this

Section 2.2: Integer literal

Section 2.3: true

Section 2.4: false

Section 2.5: nullptr

......................................................................................................................................................

.............................................................................................................................................................

...........................................................................................................................................

11

...........................................................................................................................................................

..........................................................................................................................................................

.......................................................................................................................................................

Chapter 3: operator precedence

........................................................................................................................

14

Section 3.1: Logical && and || operators: short-circuit

Section 3.2: Unary Operators

Section 3.3: Arithmetic operators

Section 3.4: Logical AND and OR operators

.....................................................................................................................................

..............................................................................................................................

............................................................................................................

..............................................................................................

14

Chapter 4: Floating Point Arithmetic

...............................................................................................................

17

Section 4.1: Floating Point Numbers are Weird

........................................................................................................

17

Chapter 5: Bit Operators

Section 5.1: | - bitwise OR

Section 5.2: ^ - bitwise XOR (exclusive OR)

Section 5.3: & - bitwise AND

Section 5.4: << - left shift

Section 5.5: >> - right shift

Chapter 6: Bit Manipulation

........................................................................................................................................

............................................................................................................................................

..............................................................................................................

18

.......................................................................................................................................

.............................................................................................................................................

..........................................................................................................................................

Section 6.1: Remove rightmost set bit

Section 6.2: Set all bits

Section 6.3: Toggling a bit

Section 6.4: Checking a bit

Section 6.5: Counting bits set

Section 6.6: Check if an integer is a power of 2

Section 6.7: Setting a bit

Section 6.8: Clearing a bit

Section 6.9: Changing the nth bit to x

Section 6.10: Bit Manipulation Application: Small to Capital Letter

...................................................................................................................................

.......................................................................................................................

................................................................................................................................................

..........................................................................................................................................

.........................................................................................................................................

.....................................................................................................................................

.......................................................................................................

.............................................................................................................................................

...........................................................................................................................................

.......................................................................................................................

........................................................................

26

Chapter 7: Bit fields

...................................................................................................................................................

27

Section 7.1: Declaration and Usage

...........................................................................................................................

27

Chapter 8: Arrays

.......................................................................................................................................................

28

....................................................................................................................................

28

Section 8.1: Array initialization

Section 8.2: A fixed size raw array matrix (that is, a 2D raw array)

Section 8.3: Dynamically sized raw array

Section 8.4: Array size: type safe at compile time

Section 8.5: Expanding dynamic size array by using std::vector

.................................................................................................................

...................................................................................................

...........................................................................

......................................................................

29

2

3

5

5

11

11

12

13

Publicité

13

15

15

16

18

18

20

20

21

23

23

23

23

23

24

25

25

25

25

29

30

31

Section 8.6: A dynamic size matrix using std::vector for storage

..........................................................................

32

Chapter 9: Iterators

Section 9.1: Overview

Section 9.2: Vector Iterator

Section 9.3: Map Iterator

Section 9.4: Reverse Iterators

Section 9.5: Stream Iterators

Section 9.6: C Iterators (Pointers)

Section 9.7: Write your own generator-backed iterator

...................................................................................................................................................

...................................................................................................................................................

........................................................................................................................................

............................................................................................................................................

....................................................................................................................................

......................................................................................................................................

..............................................................................................................................

.........................................................................................

41

Chapter 10: Basic input/output in c++

Section 10.1: user input and standard output

.............................................................................................................

43

...........................................................................................................

43

Chapter 11: Loops

........................................................................................................................................................

44

....................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................

..........................................................................................................................................

Section 11.1: Range-Based For

Section 11.2: For loop

Section 11.3: While loop

Section 11.4: Do-while loop

Section 11.5: Loop Control statements : Break and Continue

Section 11.6: Declaration of variables in conditions

Section 11.7: Range-for over a sub-range

..................................................................................

50

.................................................................................................

.................................................................................................................

Chapter 12: File I/O

....................................................................................................................................................

54

................................................................................................

..................................................................................

.............................................................

60

.......................................................................................................................................

........................................................................................................................................

...............................................................................................................................

.....................................................................................................................................

Section 12.1: Writing to a file

Section 12.2: Opening a file

Section 12.3: Reading from a file

Section 12.4: Opening modes

Section 12.5: Reading an ASCII file into a std::string

Section 12.6: Writing files with non-standard locale settings

Section 12.7: Checking end of file inside a loop condition, bad practice?

Section 12.8: Flushing a stream

Section 12.9: Reading a file into a container

Section 12.10: Copying a file

Section 12.11: Closing a file

Section 12.12: Reading a struct from a formatted text file

..................................................................................................................................

.............................................................................................................

.......................................................................................................................................

..........................................................................................................................................

....................................................................................

.........................................................................................................................................

........................................................................................................................................

Chapter 13: C++ Streams

Section 13.1: String streams

Section 13.2: Printing collections with iostream

Chapter 14: Stream manipulators

Section 14.1: Stream manipulators

Section 14.2: Output stream manipulators

Section 14.3: Input stream manipulators

........................................................................................................

66

.....................................................................................................................

.............................................................................................................................

...............................................................................................................

73

...................................................................................................................

75

Chapter 15: Flow Control

.........................................................................................................................................

77

Section 15.1: case

Section 15.2: switch

Section 15.3: catch

Section 15.4: throw

Section 15.5: default

Section 15.6: try

Section 15.7: if

Section 15.8: else

Section 15.9: Conditional Structures: if, if..else

.........................................................................................................................................................

......................................................................................................................................................

.......................................................................................................................................................

.......................................................................................................................................................

....................................................................................................................................................

............................................................................................................................................................

Publicité

...............................................................................................................................................................

..........................................................................................................................................................

...........................................................................................................

80

35

35

38

38

39

40

40

44

46

48

49

51

52

54

54

55

57

58

59

61

61

62

62

63

65

65

68

68

77

77

77

78

79

79

79

80

.......................................................................................................................................................

81

...................................................................................

81

.....................................................................................................................................................

Section 15.10: goto

Section 15.11: Jump statements : break, continue, goto, exit

Section 15.12: return

Chapter 16: Metaprogramming

Section 16.1: Calculating Factorials

Section 16.2: Iterating over a parameter pack

Section 16.3: Iterating with std::integer_sequence

Section 16.4: Tag Dispatching

Section 16.5: Detect Whether Expression is Valid

Section 16.6: If-then-else

Section 16.7: Manual distinction of types when given any type T

Section 16.8: Calculating power with C++11 (and higher)

Section 16.9: Generic Min/Max with variable argument count

...........................................................................................................................

............................................................................................................................

.........................................................................................................

...................................................................................................

....................................................................................................................................

.....................................................................................................

.............................................................................................................................................

..........................................................................

92

.........................................................................................

...............................................................................

Chapter 17: const keyword

....................................................................................................................................

95

Section 17.1: Avoiding duplication of code in const and non-const getter methods

Section 17.2: Const member functions

Section 17.3: Const local variables

Section 17.4: Const pointers

Chapter 18: mutable keyword

Section 18.1: mutable lambdas

Section 18.2: non-static class member modifier

..............................................................................................................................

...................................................................................................................................

......................................................................................................

......................................................................................................................

.............................................................................................................................

........................................................................................................................................

............................................

95

Chapter 19: Friend keyword

Section 19.1: Friend function

Section 19.2: Friend method

Section 19.3: Friend class

Chapter 20: Type Keywords

................................................................................................................................

.....................................................................................................................................

.....................................................................................................................................

..........................................................................................................................................

...............................................................................................................................

104

Section 20.1: class

Section 20.2: enum

Section 20.3: struct

Section 20.4: union

......................................................................................................................................................

104

....................................................................................................................................................

105

....................................................................................................................................................

....................................................................................................................................................

Chapter 21: Basic Type Keywords

....................................................................................................................

108

Section 21.1: char

Section 21.2: char16_t

Section 21.3: char32_t

Section 21.4: int

Section 21.5: void

Section 21.6: wchar_t

Section 21.7: float

Section 21.8: double

Section 21.9: long

Section 21.10: short

Section 21.11: bool

.......................................................................................................................................................

................................................................................................................................................

...............................................................................................................................................

108

..........................................................................................................................................................

.......................................................................................................................................................

................................................................................................................................................

Publicité

.......................................................................................................................................................

...................................................................................................................................................

.......................................................................................................................................................

....................................................................................................................................................

......................................................................................................................................................

Chapter 22: Variable Declaration Keywords

..............................................................................................

111

Section 22.1: decltype

Section 22.2: const

Section 22.3: volatile

Section 22.4: signed

Section 22.5: unsigned

Chapter 23: Keywords

...............................................................................................................................................

....................................................................................................................................................

.................................................................................................................................................

..................................................................................................................................................

..............................................................................................................................................

............................................................................................................................................

114

84

86

86

88

89

90

90

92

93

94

96

97

97

99

99

99

101

101

102

102

106

106

108

108

108

108

109

109

109

109

110

110

111

111

112

112

112

Section 23.1: asm

Section 23.2: Dierent keywords

Section 23.3: typename

Section 23.4: explicit

Section 23.5: sizeof

Section 23.6: noexcept

.......................................................................................................................................................

114

.............................................................................................................................

114

............................................................................................................................................

..................................................................................................................................................

....................................................................................................................................................

..............................................................................................................................................

Chapter 24: Returning several values from a function

.......................................................................

122

Section 24.1: Using std::tuple

Section 24.2: Structured Bindings

Section 24.3: Using struct

Section 24.4: Using Output Parameters

Section 24.5: Using a Function Object Consumer

Section 24.6: Using std::pair

Section 24.7: Using std::array

Section 24.8: Using Output Iterator

Section 24.9: Using std::vector

Chapter 25: Polymorphism

Section 25.1: Define polymorphic classes

Section 25.2: Safe downcasting

Section 25.3: Polymorphism & Destructors

....................................................................................................................................

............................................................................................................................

.........................................................................................................................................

..................................................................................................................

..................................................................................................

126

.....................................................................................................................................

...................................................................................................................................

.........................................................................................................................

.................................................................................................................................

..................................................................................................................................

...............................................................................................................

...............................................................................................................................

............................................................................................................

131

Chapter 26: References

.........................................................................................................................................

133

Section 26.1: Defining a reference

...........................................................................................................................

133

Chapter 27: Value and Reference Semantics

............................................................................................

134

Section 27.1: Definitions

Section 27.2: Deep copying and move support

............................................................................................................................................

134

.....................................................................................................

134

Chapter 28: C++ function "call by value" vs. "call by reference"

....................................................

138

Section 28.1: Call by value

........................................................................................................................................

138

Chapter 29: Copying vs Assignment

Section 29.1: Assignment Operator

Section 29.2: Copy Constructor

Section 29.3: Copy Constructor Vs Assignment Constructor

...............................................................................................................

Publicité

.........................................................................................................................

...............................................................................................................................

...............................................................................

141

Chapter 30: Pointers

...............................................................................................................................................

143

Section 30.1: Pointer Operations

Section 30.2: Pointer basics

Section 30.3: Pointer Arithmetic

Chapter 31: Pointers to members

..............................................................................................................................

143

......................................................................................................................................

143

...............................................................................................................................

145

.....................................................................................................................

147

Section 31.1: Pointers to static member functions

Section 31.2: Pointers to member functions

Section 31.3: Pointers to member variables

Section 31.4: Pointers to static member variables

..................................................................................................

...........................................................................................................

............................................................................................................

.................................................................................................

148

Chapter 32: The This Pointer

..............................................................................................................................

150

...........................................................................................................................................

Section 32.1: this Pointer

Section 32.2: Using the this Pointer to Access Member Data

Section 32.3: Using the this Pointer to Dierentiate Between Member Data and Parameters

Section 32.4: this Pointer CV-Qualifiers

Section 32.5: this Pointer Ref-Qualifiers

...................................................................................................................

..................................................................................................................

...............................................................................

........................

152

Chapter 33: Smart Pointers

.................................................................................................................................

158

Section 33.1: Unique ownership (std::unique_ptr)

Section 33.2: Sharing ownership (std::shared_ptr)

..................................................................................................

158

................................................................................................

159

118

119

119

120

122

123

124

125

127

127

127

128

129

129

130

140

140

140

147

147

148

150

152

153

156

Section 33.3: Sharing with temporary ownership (std::weak_ptr)

Section 33.4: Using custom deleters to create a wrapper to a C interface

Section 33.5: Unique ownership without move semantics (auto_ptr)

Section 33.6: Casting std::shared_ptr pointers

Section 33.7: Writing a smart pointer: value_ptr

Section 33.8: Getting a shared_ptr referring to this

.......................................................................................................

...................................................................................................

..............................................................................................

.......................................................................

161

........................................................

163

.................................................................

Chapter 34: Classes/Structures

.......................................................................................................................

170

..........................................................................................................................................

....................................................................................................................

.................................................................................................................................

..........................................................................................................................................

............................................................................................................................................

..............................................................................................................................

Section 34.1: Class basics

Section 34.2: Final classes and structs

Section 34.3: Access specifiers

Section 34.4: Inheritance

Section 34.5: Friendship

Section 34.6: Virtual Inheritance

Section 34.7: Private inheritance: restricting base class interface

Section 34.8: Accessing class members

Section 34.9: Member Types and Aliases

Section 34.10: Nested Classes/Structures

Section 34.11: Unnamed struct/class

Section 34.12: Static class members

Section 34.13: Multiple Inheritance

Section 34.14: Non-static member functions

.......................................................................

176

.................................................................................................................

...............................................................................................................

...............................................................................................................

.......................................................................................................................

........................................................................................................................

...........................................................................................................................

..........................................................................................................

Chapter 35: Function Overloading

...................................................................................................................

195

Section 35.1: What is Function Overloading?

Section 35.2: Return Type in Function Overloading

Section 35.3: Member Function cv-qualifier Overloading

..........................................................................................................<...