| Introduction |
|
xli | |
|
PART I INTRODUCTION TO PROFESSIONAL C++ |
|
|
|
Chapter 1 A Crash Course In C++ And The STL |
|
|
3 | (44) |
|
|
|
4 | (1) |
|
The Obligatory Hello, World |
|
|
4 | (1) |
|
|
|
4 | (1) |
|
|
|
5 | (1) |
|
|
|
6 | (1) |
|
|
|
6 | (1) |
|
|
|
7 | (2) |
|
|
|
9 | (2) |
|
|
|
11 | (1) |
|
|
|
11 | (2) |
|
|
|
13 | (1) |
|
|
|
13 | (2) |
|
|
|
15 | (1) |
|
|
|
15 | (1) |
|
|
|
16 | (1) |
|
|
|
16 | (1) |
|
|
|
17 | (1) |
|
Logical Evaluation Operators |
|
|
17 | (1) |
|
|
|
18 | (2) |
|
|
|
20 | (1) |
|
|
|
20 | (1) |
|
|
|
20 | (1) |
|
|
|
21 | (1) |
|
|
|
21 | (1) |
|
|
|
21 | (1) |
|
|
|
22 | (1) |
|
Alternative Function Syntax |
|
|
23 | (1) |
|
Function Return Type Deduction |
|
|
23 | (1) |
|
|
|
24 | (1) |
|
|
|
24 | (1) |
|
|
|
24 | (1) |
|
|
|
24 | (1) |
|
|
|
25 | (1) |
|
Pointers and Dynamic Memory |
|
|
25 | (1) |
|
|
|
25 | (1) |
|
|
|
26 | (1) |
|
Dynamically Allocated Arrays |
|
|
27 | (1) |
|
|
|
28 | (1) |
|
|
|
28 | (1) |
|
|
|
29 | (1) |
|
|
|
30 | (1) |
|
|
|
30 | (2) |
|
|
|
32 | (1) |
|
|
|
32 | (1) |
|
const to Protect Parameters |
|
|
32 | (1) |
|
|
|
32 | (1) |
|
|
|
33 | (1) |
|
|
|
33 | (1) |
|
C++ as an Object-Oriented Language |
|
|
34 | (1) |
|
|
|
34 | (2) |
|
|
|
36 | (1) |
|
|
|
36 | (1) |
|
Your First Useful C++ Program |
|
|
37 | (1) |
|
An Employee Records System |
|
|
37 | (1) |
|
|
|
38 | (1) |
|
|
|
38 | (1) |
|
|
|
39 | (2) |
|
|
|
41 | (1) |
|
|
|
41 | (1) |
|
|
|
41 | (1) |
|
|
|
42 | (1) |
|
|
|
43 | (1) |
|
|
|
44 | (1) |
|
|
|
44 | (2) |
|
|
|
46 | (1) |
|
|
|
46 | (1) |
|
Chapter 2 Working With Strings |
|
|
47 | (10) |
|
|
|
47 | (1) |
|
|
|
48 | (2) |
|
|
|
50 | (1) |
|
|
|
51 | (1) |
|
What Is Wrong with C-Style Strings? |
|
|
51 | (1) |
|
|
|
51 | (2) |
|
|
|
53 | (1) |
|
|
|
53 | (1) |
|
|
|
54 | (1) |
|
|
|
55 | (1) |
|
|
|
55 | (2) |
|
Chapter 3 Coding With Style |
|
|
57 | (22) |
|
The Importance of Looking Good |
|
|
57 | (1) |
|
|
|
58 | (1) |
|
|
|
58 | (1) |
|
|
|
58 | (1) |
|
Reasons to Write Comments |
|
|
58 | (1) |
|
Commenting to Explain Usage |
|
|
58 | (2) |
|
Commenting to Explain Complicated Code |
|
|
60 | (1) |
|
Commenting to Convey Metainformation |
|
|
61 | (1) |
|
|
|
62 | (1) |
|
|
|
62 | (2) |
|
|
|
64 | (1) |
|
|
|
64 | (1) |
|
|
|
65 | (1) |
|
|
|
66 | (1) |
|
|
|
66 | (1) |
|
|
|
66 | (1) |
|
Decomposition through Refactoring |
|
|
67 | (1) |
|
|
|
67 | (1) |
|
Decomposition in This Book |
|
|
68 | (1) |
|
|
|
68 | (1) |
|
|
|
68 | (1) |
|
|
|
69 | (1) |
|
|
|
69 | (1) |
|
|
|
69 | (1) |
|
|
|
70 | (1) |
|
|
|
70 | (1) |
|
|
|
70 | (1) |
|
|
|
70 | (1) |
|
Using Language Features with Style |
|
|
71 | (1) |
|
|
|
71 | (1) |
|
Use References Instead of Pointers |
|
|
71 | (1) |
|
|
|
72 | (1) |
|
|
|
72 | (1) |
|
The Curly Brace Alignment Debate |
|
|
72 | (2) |
|
Coming to Blows over Spaces and Parentheses |
|
|
74 | (1) |
|
|
|
74 | (1) |
|
|
|
74 | (1) |
|
|
|
75 | (4) |
|
PART II PROFESSIONAL C++ SOFTWARE DESIGN |
|
|
|
Chapter 4 Designing Professional C++ Programs |
|
|
79 | (28) |
|
What Is Programming Design? |
|
|
80 | (1) |
|
The Importance of Programming Design |
|
|
80 | (2) |
|
|
|
82 | (1) |
|
|
|
83 | (1) |
|
|
|
84 | (1) |
|
Benefiting from Abstraction |
|
|
84 | (1) |
|
Incorporating Abstraction in Your Design |
|
|
84 | (1) |
|
|
|
85 | (1) |
|
|
|
86 | (1) |
|
|
|
86 | (1) |
|
|
|
87 | (1) |
|
|
|
87 | (1) |
|
|
|
87 | (1) |
|
Deciding Whether or Not to Reuse Code |
|
|
88 | (1) |
|
Advantages to Reusing Code |
|
|
88 | (1) |
|
Disadvantages to Reusing Code |
|
|
89 | (1) |
|
Putting It Together to Make a Decision |
|
|
90 | (1) |
|
Strategies for Reusing Code |
|
|
90 | (1) |
|
Understand the Capabilities and Limitations |
|
|
90 | (1) |
|
Understand the Performance |
|
|
91 | (3) |
|
Understand Platform Limitations |
|
|
94 | (1) |
|
Understand Licensing and Support |
|
|
94 | (1) |
|
|
|
94 | (1) |
|
|
|
95 | (1) |
|
Bundling Third-Party Applications |
|
|
95 | (1) |
|
|
|
96 | (1) |
|
The Open-Source Movements |
|
|
96 | (1) |
|
Finding and Using Open-Source Libraries |
|
|
96 | (1) |
|
Guidelines for Using Open-Source Code |
|
|
97 | (1) |
|
|
|
97 | (1) |
|
|
|
97 | (1) |
|
Deciding Whether or Not to Use the STL |
|
|
98 | (1) |
|
Designing with Patterns and Techniques |
|
|
98 | (1) |
|
Designing a Chess Program |
|
|
99 | (1) |
|
|
|
99 | (1) |
|
|
|
99 | (1) |
|
Divide the Program into Subsystems |
|
|
99 | (2) |
|
|
|
101 | (1) |
|
Specify Class Hierarchies for Each Subsystem |
|
|
101 | (1) |
|
Specify Classes, Data Structures, Algorithms, and Patterns for Each Subsystem |
|
|
102 | (2) |
|
Specify Error Handling for Each Subsystem |
|
|
104 | (1) |
|
|
|
105 | (2) |
|
Chapter 5 Designing With Objects |
|
|
107 | (20) |
|
Am I Thinking Procedurally? |
|
|
108 | (1) |
|
The Object-Oriented Philosophy |
|
|
108 | (1) |
|
|
|
108 | (1) |
|
|
|
109 | (1) |
|
|
|
109 | (1) |
|
|
|
110 | (1) |
|
|
|
110 | (1) |
|
Living in a World of Objects |
|
|
111 | (1) |
|
|
|
111 | (1) |
|
|
|
112 | (1) |
|
|
|
113 | (1) |
|
|
|
113 | (1) |
|
The Is-A Relationship (Inheritance) |
|
|
114 | (1) |
|
|
|
115 | (1) |
|
Polymorphism versus Code Reuse |
|
|
116 | (1) |
|
The Fine Line between Has-A and Is-A |
|
|
116 | (3) |
|
|
|
119 | (1) |
|
|
|
120 | (1) |
|
|
|
121 | (1) |
|
|
|
122 | (1) |
|
|
|
122 | (1) |
|
Interface versus Implementation |
|
|
123 | (1) |
|
Deciding on an Exposed Interface |
|
|
123 | (1) |
|
|
|
123 | (1) |
|
|
|
124 | (1) |
|
|
|
125 | (1) |
|
Designing a Successful Abstraction |
|
|
125 | (1) |
|
|
|
126 | (1) |
|
Chapter 6 Designing For Reuse |
|
|
127 | (16) |
|
|
|
127 | (1) |
|
How to Design Reusable Code |
|
|
128 | (1) |
|
|
|
129 | (1) |
|
Structure Your Code for Optimal Reuse |
|
|
130 | (1) |
|
Avoid Combining Unrelated or Logically Separate Concepts |
|
|
130 | (2) |
|
Use Templates for Generic Data Structures and Algorithms |
|
|
132 | (2) |
|
Provide Appropriate Checks and Safeguards |
|
|
134 | (1) |
|
|
|
135 | (1) |
|
Design Interfaces That Are Easy to Use |
|
|
135 | (3) |
|
Design General-Purpose Interfaces |
|
|
138 | (1) |
|
Reconciling Generality and Ease of Use |
|
|
139 | (1) |
|
Supply Multiple Interfaces |
|
|
139 | (1) |
|
Make Common Functionality Easy to Use |
|
|
140 | (1) |
|
|
|
140 | (3) |
|
PART III CODING THE PROFESSIONAL WAY |
|
|
|
Chapter 7 Gaining Proficiency With Classes And Objects |
|
|
143 | (34) |
|
Introducing the Spreadsheet Example |
|
|
144 | (1) |
|
|
|
144 | (1) |
|
|
|
144 | (1) |
|
|
|
145 | (1) |
|
|
|
145 | (2) |
|
|
|
147 | (1) |
|
|
|
147 | (1) |
|
|
|
148 | (1) |
|
|
|
148 | (2) |
|
|
|
150 | (1) |
|
|
|
151 | (1) |
|
|
|
151 | (1) |
|
|
|
151 | (1) |
|
|
|
152 | (1) |
|
|
|
153 | (1) |
|
|
|
153 | (1) |
|
|
|
154 | (1) |
|
Providing Multiple Constructors |
|
|
155 | (1) |
|
|
|
156 | (4) |
|
|
|
160 | (2) |
|
|
|
162 | (3) |
|
Initializer-List Constructors |
|
|
165 | (2) |
|
In-Class Member Initializers |
|
|
167 | (1) |
|
|
|
167 | (1) |
|
Summary of Compiler-Generated Constructors |
|
|
168 | (1) |
|
|
|
169 | (1) |
|
|
|
170 | (1) |
|
Declaring an Assignment Operator |
|
|
171 | (1) |
|
Defining an Assignment Operator |
|
|
172 | (1) |
|
Explicitly Defaulted and Deleted Assignment Operator |
|
|
173 | (1) |
|
Distinguishing Copying from Assignment |
|
|
173 | (1) |
|
|
|
173 | (1) |
|
Copy Constructors and Object Members |
|
|
174 | (1) |
|
|
|
175 | (2) |
|
Chapter 8 Mastering Classes And Objects |
|
|
177 | (40) |
|
Dynamic Memory Allocation in Objects |
|
|
178 | (1) |
|
|
|
178 | (2) |
|
Freeing Memory with Destructors |
|
|
180 | (1) |
|
Handling Copying and Assignment |
|
|
180 | (5) |
|
The Spreadsheet Copy Constructor |
|
|
185 | (1) |
|
The Spreadsheet Assignment Operator |
|
|
185 | (2) |
|
Common Helper Routines for Copy Constructor and Assignment Operator |
|
|
187 | (1) |
|
Disallowing Assignment and Pass-By-Value |
|
|
188 | (1) |
|
Different Kinds of Data Members |
|
|
188 | (1) |
|
|
|
188 | (1) |
|
Accessing static Data Members within Class Methods |
|
|
189 | (1) |
|
Accessing static Data Members Outside Methods |
|
|
190 | (1) |
|
|
|
190 | (1) |
|
|
|
191 | (1) |
|
const Reference Data Members |
|
|
192 | (1) |
|
|
|
193 | (1) |
|
|
|
193 | (1) |
|
|
|
194 | (1) |
|
|
|
195 | (1) |
|
|
|
195 | (2) |
|
|
|
197 | (1) |
|
|
|
197 | (2) |
|
|
|
199 | (2) |
|
Enumerated Types Inside Classes |
|
|
201 | (1) |
|
|
|
202 | (1) |
|
|
|
203 | (1) |
|
Example: Implementing Addition for SpreadsheetCells |
|
|
203 | (1) |
|
First Attempt: The add Method |
|
|
203 | (1) |
|
Second Attempt: Overloaded operator+ as a Method |
|
|
204 | (2) |
|
Third Attempt: Global operator+ |
|
|
206 | (1) |
|
Overloading Arithmetic Operators |
|
|
207 | (1) |
|
Overloading the Arithmetic Shorthand Operators |
|
|
208 | (2) |
|
Overloading Comparison Operators |
|
|
210 | (1) |
|
Building Types with Operator Overloading |
|
|
211 | (1) |
|
Building Stable Interfaces |
|
|
212 | (1) |
|
Using Interface and Implementation Classes |
|
|
212 | (3) |
|
|
|
215 | (2) |
|
Chapter 9 Discovering Inheritance Techniques |
|
|
217 | (56) |
|
Building Classes with Inheritance |
|
|
218 | (1) |
|
|
|
218 | (1) |
|
A Client's View of Inheritance |
|
|
219 | (1) |
|
A Derived Class's View of Inheritance |
|
|
220 | (1) |
|
|
|
221 | (1) |
|
|
|
222 | (1) |
|
How I Learned to Stop Worrying and Make Everything virtual |
|
|
222 | (1) |
|
Syntax for Overriding a Method |
|
|
223 | (1) |
|
A Client's View of Overridden Methods |
|
|
223 | (2) |
|
|
|
225 | (1) |
|
|
|
225 | (1) |
|
The WeatherPrediction Class |
|
|
225 | (1) |
|
Adding Functionality in a Derived Class |
|
|
226 | (2) |
|
Replacing Functionality in a Derived Class |
|
|
228 | (1) |
|
|
|
229 | (1) |
|
|
|
229 | (1) |
|
|
|
230 | (2) |
|
Referring to Parent Names |
|
|
232 | (3) |
|
|
|
235 | (1) |
|
Inheritance for Polymorphism |
|
|
236 | (1) |
|
Return of the Spreadsheet |
|
|
236 | (1) |
|
Designing the Polymorphic Spreadsheet Cell |
|
|
237 | (1) |
|
The Spreadsheet Cell Base Class |
|
|
237 | (1) |
|
|
|
238 | (1) |
|
Pure Virtual Methods and Abstract Base Classes |
|
|
238 | (1) |
|
|
|
239 | (1) |
|
The Individual Derived Classes |
|
|
239 | (1) |
|
String Spreadsheet Cell Class Definition |
|
|
240 | (1) |
|
String Spreadsheet Cell Implementation |
|
|
240 | (1) |
|
Double Spreadsheet Cell Class Definition and Implementation |
|
|
241 | (1) |
|
|
|
242 | (1) |
|
|
|
243 | (1) |
|
|
|
244 | (1) |
|
Inheriting from Multiple Classes |
|
|
244 | (2) |
|
Naming Collisions and Ambiguous Base Classes |
|
|
246 | (1) |
|
|
|
246 | (1) |
|
|
|
247 | (1) |
|
Uses for Multiple Inheritance |
|
|
248 | (1) |
|
Interesting and Obscure Inheritance Issues |
|
|
249 | (1) |
|
Changing the Overridden Method's Characteristics |
|
|
249 | (1) |
|
Changing the Method Return Type |
|
|
249 | (1) |
|
Changing the Method Parameters |
|
|
250 | (3) |
|
|
|
253 | (3) |
|
Special Cases in Overriding Methods |
|
|
256 | (1) |
|
The Base Class Method Is static |
|
|
257 | (1) |
|
The Base Class Method Is Overloaded |
|
|
258 | (1) |
|
The Base Class Method Is private or protected |
|
|
259 | (1) |
|
The Base Class Method Has Default Arguments |
|
|
260 | (1) |
|
The Base Class Method Has a Different Access Level |
|
|
261 | (3) |
|
Copy Constructors and Assignment Operator in Derived Classes |
|
|
264 | (1) |
|
|
|
265 | (1) |
|
Hiding Instead of Overriding |
|
|
265 | (1) |
|
How virtual Is Implemented |
|
|
266 | (1) |
|
The Justification for virtual |
|
|
267 | (1) |
|
The Need for virtual Destructors |
|
|
267 | (1) |
|
|
|
268 | (2) |
|
|
|
270 | (1) |
|
|
|
270 | (1) |
|
|
|
271 | (2) |
|
Chapter 10 C++ Quirks, Oddities, And Incidentals |
|
|
273 | (42) |
|
|
|
274 | (1) |
|
|
|
274 | (1) |
|
|
|
275 | (1) |
|
References to Pointers and Pointers to References |
|
|
275 | (1) |
|
|
|
276 | (1) |
|
|
|
276 | (1) |
|
|
|
277 | (1) |
|
Pass-by-Reference Versus Pass-by-Value |
|
|
277 | (1) |
|
|
|
278 | (1) |
|
Deciding between References and Pointers |
|
|
278 | (3) |
|
|
|
281 | (2) |
|
|
|
283 | (3) |
|
|
|
286 | (1) |
|
|
|
286 | (1) |
|
const Variables and Parameters |
|
|
287 | (2) |
|
|
|
289 | (1) |
|
|
|
289 | (2) |
|
|
|
291 | (1) |
|
static Data Members and Methods |
|
|
291 | (1) |
|
|
|
291 | (2) |
|
static Variables in Functions |
|
|
293 | (1) |
|
Order of Initialization of Nonlocal Variables |
|
|
294 | (1) |
|
Order of Destruction of Nonlocal Variables |
|
|
294 | (1) |
|
|
|
295 | (1) |
|
|
|
295 | (1) |
|
typedefs for Function Pointers |
|
|
296 | (1) |
|
|
|
297 | (1) |
|
|
|
297 | (1) |
|
|
|
298 | (1) |
|
|
|
298 | (1) |
|
|
|
299 | (1) |
|
|
|
300 | (1) |
|
|
|
301 | (1) |
|
|
|
302 | (1) |
|
|
|
303 | (1) |
|
|
|
303 | (2) |
|
|
|
305 | (1) |
|
Explicit Conversion Operators |
|
|
305 | (1) |
|
|
|
306 | (1) |
|
|
|
307 | (1) |
|
Standard User-Defined Literals |
|
|
308 | (1) |
|
|
|
309 | (1) |
|
|
|
310 | (1) |
|
Variable-Length Argument Lists |
|
|
310 | (1) |
|
|
|
311 | (1) |
|
Why You Shouldn't Use C-Style Variable-Length Argument Lists |
|
|
312 | (1) |
|
|
|
312 | (1) |
|
|
|
313 | (2) |
|
Chapter 11 Writing Generic Code With Templates |
|
|
315 | (30) |
|
|
|
316 | (1) |
|
|
|
317 | (1) |
|
|
|
317 | (1) |
|
|
|
317 | (3) |
|
|
|
320 | (3) |
|
|
|
323 | (1) |
|
|
|
324 | (1) |
|
How the Compiler Processes Templates |
|
|
324 | (1) |
|
|
|
325 | (1) |
|
Template Requirements on Types |
|
|
325 | (1) |
|
Distributing Template Code between Files |
|
|
325 | (1) |
|
Template Definitions in Header Files |
|
|
325 | (1) |
|
Template Definitions in Source Files |
|
|
326 | (1) |
|
|
|
327 | (1) |
|
Non-Type Template Parameters |
|
|
327 | (2) |
|
Default Values for Type Parameters |
|
|
329 | (1) |
|
|
|
330 | (2) |
|
Method Templates with Non-Type Parameters |
|
|
332 | (2) |
|
Class Template Specialization |
|
|
334 | (2) |
|
Deriving from Class Templates |
|
|
336 | (1) |
|
Inheritance versus Specialization |
|
|
337 | (1) |
|
|
|
338 | (1) |
|
Alternative Function Syntax |
|
|
338 | (1) |
|
|
|
339 | (1) |
|
Function Template Specialization |
|
|
340 | (1) |
|
Function Template Overloading |
|
|
341 | (1) |
|
Function Template Overloading and Specialization Together |
|
|
342 | (1) |
|
Friend Function Templates of Class Templates |
|
|
342 | (1) |
|
|
|
343 | (1) |
|
|
|
343 | (2) |
|
Chapter 12 Demystifying C++ I/O |
|
|
345 | (24) |
|
|
|
346 | (1) |
|
What Is a Stream, Anyway? |
|
|
346 | (1) |
|
Stream Sources and Destinations |
|
|
347 | (1) |
|
|
|
347 | (1) |
|
|
|
347 | (1) |
|
Methods of Output Streams |
|
|
348 | (2) |
|
|
|
350 | (1) |
|
|
|
351 | (2) |
|
|
|
353 | (1) |
|
|
|
353 | (1) |
|
|
|
354 | (3) |
|
|
|
357 | (1) |
|
|
|
358 | (1) |
|
Input and Output with Objects |
|
|
359 | (1) |
|
|
|
360 | (2) |
|
|
|
362 | (1) |
|
Jumping around with seek() and tell() |
|
|
363 | (2) |
|
|
|
365 | (1) |
|
|
|
366 | (1) |
|
|
|
367 | (2) |
|
Chapter 13 Handling Errors |
|
|
369 | (42) |
|
|
|
370 | (1) |
|
What Are Exceptions, Anyway? |
|
|
370 | (1) |
|
Why Exceptions in C++ Are a Good Thing |
|
|
371 | (1) |
|
|
|
372 | (1) |
|
|
|
372 | (1) |
|
Throwing and Catching Exceptions |
|
|
373 | (3) |
|
|
|
376 | (1) |
|
Catching Exception Objects by const and Reference |
|
|
377 | (1) |
|
Throwing and Catching Multiple Exceptions |
|
|
378 | (1) |
|
|
|
379 | (1) |
|
|
|
380 | (1) |
|
|
|
380 | (2) |
|
|
|
382 | (1) |
|
|
|
383 | (2) |
|
Changing the Throw List in Overridden Methods |
|
|
385 | (1) |
|
|
|
386 | (1) |
|
Exceptions and Polymorphism |
|
|
387 | (1) |
|
The Standard Exception Hierarchy |
|
|
387 | (1) |
|
Catching Exceptions in a Class Hierarchy |
|
|
388 | (2) |
|
Writing Your Own Exception Classes |
|
|
390 | (2) |
|
|
|
392 | (2) |
|
Stack Unwinding and Cleanup |
|
|
394 | (2) |
|
|
|
396 | (1) |
|
Catch, Cleanup, and Rethrow |
|
|
396 | (1) |
|
Common Error-Handling Issues |
|
|
397 | (1) |
|
|
|
397 | (1) |
|
|
|
398 | (1) |
|
Customizing Memory Allocation Failure Behavior |
|
|
399 | (1) |
|
|
|
400 | (2) |
|
Function-Try-Blocks for Constructors |
|
|
402 | (2) |
|
|
|
404 | (1) |
|
|
|
405 | (4) |
|
|
|
409 | (2) |
|
Chapter 14 Overloading C++ Operators |
|
|
411 | (32) |
|
Overview of Operator Overloading |
|
|
412 | (1) |
|
|
|
412 | (1) |
|
Limitations to Operator Overloading |
|
|
412 | (1) |
|
Choices in Operator Overloading |
|
|
413 | (1) |
|
Method or Global Function |
|
|
413 | (1) |
|
|
|
414 | (1) |
|
|
|
414 | (1) |
|
|
|
415 | (1) |
|
Operators You Shouldn't Overload |
|
|
415 | (1) |
|
Summary of Overloadable Operators |
|
|
415 | (4) |
|
|
|
419 | (1) |
|
|
|
419 | (1) |
|
Overloading the Arithmetic Operators |
|
|
420 | (1) |
|
Overloading Unary Minus and Unary Plus |
|
|
420 | (1) |
|
Overloading Increment and Decrement |
|
|
420 | (1) |
|
Overloading the Bitwise and Binary Logical Operators |
|
|
421 | (1) |
|
Overloading the Insertion and Extraction Operators |
|
|
422 | (1) |
|
Overloading the Subscripting Operator |
|
|
423 | (3) |
|
Providing Read-Only Access with operator[ ] |
|
|
426 | (1) |
|
Non-Integral Array Indices |
|
|
427 | (1) |
|
Overloading the Function Call Operator |
|
|
428 | (1) |
|
Overloading the Dereferencing Operators |
|
|
429 | (1) |
|
|
|
430 | (1) |
|
|
|
431 | (1) |
|
What in the World Is operators*? |
|
|
432 | (1) |
|
Writing Conversion Operators |
|
|
432 | (1) |
|
Ambiguity Problems with Conversion Operators |
|
|
433 | (1) |
|
Conversions for Boolean Expressions |
|
|
434 | (2) |
|
Overloading the Memory Allocation and Deallocation Operators |
|
|
436 | (1) |
|
How new and delete Really Work |
|
|
436 | (1) |
|
The New-Expression and operator new |
|
|
437 | (1) |
|
The Delete-Expression and operator delete |
|
|
437 | (1) |
|
Overloading operator new and operator delete |
|
|
437 | (3) |
|
Explicitly Deleting/Defaulting operator new and operator delete |
|
|
440 | (1) |
|
Overloading operator new and operator delete with Extra Parameters |
|
|
440 | (2) |
|
|
|
442 | (1) |
|
Chapter 15 Overview Of The C++ Standard Library |
|
|
443 | (24) |
|
|
|
444 | (1) |
|
|
|
444 | (1) |
|
Use of Operator Overloading |
|
|
444 | (1) |
|
Overview of the C++ Standard Library |
|
|
445 | (1) |
|
|
|
445 | (1) |
|
|
|
445 | (1) |
|
|
|
445 | (1) |
|
|
|
446 | (1) |
|
|
|
446 | (1) |
|
|
|
446 | (1) |
|
|
|
447 | (1) |
|
|
|
447 | (1) |
|
|
|
447 | (1) |
|
|
|
447 | (1) |
|
|
|
448 | (1) |
|
|
|
448 | (1) |
|
|
|
448 | (1) |
|
The Standard Template Library |
|
|
448 | (1) |
|
|
|
448 | (8) |
|
|
|
456 | (9) |
|
What's Missing from the STL |
|
|
465 | (1) |
|
|
|
465 | (2) |
|
Chapter 16 Understanding Containers And Iterators |
|
|
467 | (64) |
|
|
|
468 | (1) |
|
|
|
468 | (2) |
|
Exceptions and Error Checking |
|
|
470 | (1) |
|
|
|
470 | (2) |
|
Common Iterator typedefs and Methods |
|
|
472 | (1) |
|
|
|
473 | (1) |
|
|
|
473 | (1) |
|
|
|
473 | (2) |
|
|
|
475 | (11) |
|
vector Example: A Round-Robin Class |
|
|
486 | (4) |
|
The vector<bool> Specialization |
|
|
490 | (1) |
|
|
|
491 | (1) |
|
|
|
491 | (1) |
|
|
|
492 | (1) |
|
|
|
492 | (1) |
|
Adding and Removing Elements |
|
|
492 | (1) |
|
|
|
492 | (1) |
|
|
|
492 | (3) |
|
|
|
495 | (2) |
|
|
|
497 | (1) |
|
|
|
498 | (1) |
|
|
|
498 | (1) |
|
|
|
499 | (1) |
|
queue Example: A Network Packet Buffer |
|
|
499 | (2) |
|
|
|
501 | (1) |
|
priority queue Operations |
|
|
502 | (1) |
|
priority queue Example: An Error Correlator |
|
|
502 | (2) |
|
|
|
504 | (1) |
|
|
|
504 | (1) |
|
stack Example: Revised Error Correlator |
|
|
504 | (1) |
|
|
|
504 | (1) |
|
|
|
504 | (1) |
|
|
|
505 | (1) |
|
|
|
506 | (1) |
|
|
|
506 | (2) |
|
|
|
508 | (1) |
|
|
|
509 | (1) |
|
|
|
509 | (1) |
|
map Example: Bank Account |
|
|
510 | (2) |
|
|
|
512 | (1) |
|
multimap Example: Buddy Lists |
|
|
512 | (3) |
|
|
|
515 | (1) |
|
set Example: Access Control List |
|
|
515 | (1) |
|
|
|
516 | (1) |
|
Unordered Associative Containers/Hash Tables |
|
|
516 | (1) |
|
|
|
517 | (2) |
|
|
|
519 | (2) |
|
Unordered_map Example: Phone Book |
|
|
521 | (1) |
|
|
|
522 | (1) |
|
Unordered_set/unordered multiset |
|
|
522 | (1) |
|
|
|
523 | (1) |
|
|
|
523 | (1) |
|
|
|
524 | (1) |
|
|
|
524 | (1) |
|
|
|
524 | (1) |
|
|
|
525 | (1) |
|
|
|
525 | (1) |
|
bitset Example: Representing Cable Channels |
|
|
526 | (3) |
|
|
|
529 | (2) |
|
Chapter 17 Mastering STL Algorithms |
|
|
531 | (44) |
|
|
|
532 | (1) |
|
The find and find if Algorithms |
|
|
532 | (3) |
|
The accumulate Algorithms |
|
|
535 | (1) |
|
Move Semantics with Algorithms |
|
|
536 | (1) |
|
|
|
536 | (1) |
|
|
|
536 | (2) |
|
|
|
538 | (1) |
|
Generic Lambda Expressions |
|
|
539 | (1) |
|
Lambda Capture Expressions |
|
|
539 | (1) |
|
Lambda Expressions as Return Type |
|
|
540 | (1) |
|
Lambda Expressions as Parameters |
|
|
541 | (1) |
|
Examples with STL Algorithms |
|
|
541 | (1) |
|
|
|
542 | (1) |
|
|
|
542 | (1) |
|
|
|
542 | (1) |
|
Arithmetic Function Objects |
|
|
543 | (1) |
|
Transparent Operator Functors |
|
|
544 | (1) |
|
Comparison Function Objects |
|
|
544 | (1) |
|
|
|
545 | (1) |
|
|
|
546 | (1) |
|
|
|
546 | (1) |
|
|
|
546 | (2) |
|
|
|
548 | (1) |
|
|
|
548 | (1) |
|
Writing Your Own Function Objects |
|
|
549 | (1) |
|
|
|
550 | (1) |
|
|
|
551 | (1) |
|
Non-Modifying Sequence Algorithms |
|
|
551 | (1) |
|
|
|
551 | (2) |
|
|
|
553 | (2) |
|
|
|
555 | (1) |
|
Modifying Sequence Algorithms |
|
|
556 | (1) |
|
|
|
556 | (2) |
|
|
|
558 | (1) |
|
|
|
559 | (1) |
|
|
|
560 | (1) |
|
|
|
561 | (1) |
|
|
|
562 | (1) |
|
|
|
562 | (1) |
|
|
|
562 | (1) |
|
|
|
562 | (2) |
|
|
|
564 | (1) |
|
|
|
565 | (1) |
|
|
|
566 | (1) |
|
|
|
566 | (3) |
|
Minimum/Maximum Algorithms |
|
|
569 | (1) |
|
Numerical Processing Algorithms |
|
|
570 | (1) |
|
|
|
570 | (1) |
|
|
|
570 | (1) |
|
Algorithms Example: Auditing Voter Registrations |
|
|
570 | (1) |
|
The Voter Registration Audit Problem Statement |
|
|
571 | (1) |
|
The auditVoterRolls Function |
|
|
571 | (1) |
|
The getDuplicates Function |
|
|
572 | (1) |
|
Testing the auditVoterRolls Function |
|
|
573 | (1) |
|
|
|
573 | (2) |
|
Chapter 18 String Localization And Regular Expressions |
|
|
575 | (26) |
|
|
|
575 | (1) |
|
Localizing String Literals |
|
|
576 | (1) |
|
|
|
576 | (1) |
|
Non-Western Character Sets |
|
|
577 | (2) |
|
|
|
579 | (1) |
|
|
|
579 | (1) |
|
|
|
580 | (1) |
|
|
|
581 | (1) |
|
|
|
582 | (1) |
|
|
|
582 | (1) |
|
|
|
582 | (1) |
|
|
|
582 | (1) |
|
|
|
583 | (1) |
|
|
|
583 | (1) |
|
|
|
584 | (1) |
|
|
|
584 | (2) |
|
|
|
586 | (1) |
|
|
|
587 | (1) |
|
|
|
587 | (1) |
|
Regular Expressions and Raw String Literals |
|
|
587 | (1) |
|
|
|
588 | (1) |
|
|
|
589 | (1) |
|
|
|
589 | (3) |
|
|
|
592 | (1) |
|
|
|
592 | (1) |
|
|
|
593 | (1) |
|
|
|
593 | (1) |
|
|
|
594 | (1) |
|
Regex_token_iterator Examples |
|
|
595 | (1) |
|
|
|
596 | (1) |
|
|
|
597 | (2) |
|
|
|
599 | (2) |
|
Chapter 19 Additional Library Utilities |
|
|
601 | (26) |
|
|
|
601 | (2) |
|
|
|
603 | (3) |
|
|
|
606 | (1) |
|
|
|
606 | (4) |
|
|
|
610 | (1) |
|
|
|
611 | (1) |
|
|
|
612 | (1) |
|
|
|
613 | (2) |
|
Random Number Engine Adapters |
|
|
615 | (1) |
|
Predefined Engines and Engine Adapters |
|
|
616 | (1) |
|
Generating Random Numbers |
|
|
616 | (2) |
|
Random Number Distributions |
|
|
618 | (3) |
|
|
|
621 | (3) |
|
|
|
624 | (3) |
|
PART IV MASTERING ADVANCED FEATURES OF C++ |
|
|
|
Chapter 20 Customizing And Extending The STL |
|
|
627 | (46) |
|
|
|
628 | (1) |
|
|
|
628 | (1) |
|
|
|
629 | (1) |
|
|
|
630 | (1) |
|
|
|
631 | (1) |
|
|
|
632 | (2) |
|
|
|
634 | (1) |
|
|
|
634 | (1) |
|
|
|
634 | (1) |
|
|
|
634 | (1) |
|
|
|
635 | (1) |
|
|
|
636 | (1) |
|
|
|
636 | (8) |
|
Making hash map an STL Container |
|
|
644 | (13) |
|
|
|
657 | (1) |
|
Note on Reversible Containers |
|
|
658 | (1) |
|
Making hash map an Associative Container |
|
|
658 | (12) |
|
Note on Sequential Containers |
|
|
670 | (1) |
|
|
|
671 | (2) |
|
Chapter 21 Advanced Templates |
|
|
673 | (34) |
|
More about Template Parameters |
|
|
673 | (1) |
|
More about Template Type Parameters |
|
|
674 | (2) |
|
Default Values for Template Type Parameters |
|
|
676 | (1) |
|
Introducing Template Template Parameters |
|
|
676 | (2) |
|
More about Non-Type Template Parameters |
|
|
678 | (1) |
|
Reference and Pointer Non-Type Template Parameters |
|
|
679 | (1) |
|
Class Template Partial Specialization |
|
|
679 | (2) |
|
Another Form of Partial Specialization |
|
|
681 | (2) |
|
Emulating Function Partial Specialization with Overloading |
|
|
683 | (1) |
|
|
|
684 | (1) |
|
|
|
685 | (1) |
|
An N-Dimensional Grid: First Attempt |
|
|
685 | (1) |
|
A Real N-Dimensional Grid |
|
|
686 | (3) |
|
|
|
689 | (1) |
|
auto and decltype with Templates |
|
|
689 | (2) |
|
|
|
691 | (1) |
|
Type-Safe Variable-Length Argument Lists |
|
|
692 | (2) |
|
Variable Number of Mixin Classes |
|
|
694 | (1) |
|
|
|
695 | (1) |
|
Factorial at Compile Time |
|
|
696 | (1) |
|
|
|
696 | (2) |
|
|
|
698 | (1) |
|
|
|
699 | (1) |
|
|
|
700 | (2) |
|
|
|
702 | (1) |
|
|
|
703 | (2) |
|
Metaprogramming Conclusion |
|
|
705 | (1) |
|
|
|
705 | (2) |
|
Chapter 22 Memory Management |
|
|
707 | (34) |
|
Working with Dynamic Memory |
|
|
708 | (1) |
|
|
|
708 | (1) |
|
Allocation and Deallocation |
|
|
709 | (1) |
|
|
|
710 | (1) |
|
What about My Good Friend malloc? |
|
|
710 | (1) |
|
When Memory Allocation Fails |
|
|
711 | (1) |
|
|
|
711 | (1) |
|
|
|
712 | (1) |
|
|
|
713 | (1) |
|
|
|
714 | (1) |
|
|
|
715 | (3) |
|
|
|
718 | (1) |
|
A Mental Model for Pointers |
|
|
718 | (1) |
|
|
|
719 | (1) |
|
|
|
720 | (1) |
|
|
|
720 | (2) |
|
Not All Pointers Are Arrays! |
|
|
722 | (1) |
|
Low-Level Memory Operations |
|
|
722 | (1) |
|
|
|
722 | (1) |
|
|
|
723 | (1) |
|
|
|
724 | (1) |
|
|
|
725 | (1) |
|
|
|
725 | (2) |
|
Pointers to Methods and Data Members |
|
|
727 | (1) |
|
|
|
727 | (1) |
|
The Old Deprecated auto_ptr |
|
|
728 | (1) |
|
The unique_ptr and shared_ptr Smart Pointers |
|
|
729 | (1) |
|
|
|
729 | (2) |
|
|
|
731 | (2) |
|
|
|
733 | (1) |
|
|
|
734 | (1) |
|
|
|
734 | (1) |
|
|
|
734 | (1) |
|
Accessing Out-of-Bounds Memory |
|
|
735 | (1) |
|
|
|
736 | (1) |
|
Finding and Fixing Memory Leaks in Windows with Visual C++ |
|
|
737 | (1) |
|
Finding and Fixing Memory Leaks in Linux with Valgrind |
|
|
738 | (1) |
|
Double-Deleting and Invalid Pointers |
|
|
739 | (1) |
|
|
|
740 | (1) |
|
Chapter 23 Multithreaded Programming With C++ |
|
|
741 | (40) |
|
|
|
742 | (1) |
|
|
|
743 | (2) |
|
|
|
745 | (1) |
|
|
|
746 | (1) |
|
|
|
746 | (1) |
|
|
|
746 | (1) |
|
Thread with Function Pointer |
|
|
746 | (2) |
|
Thread with Function Object |
|
|
748 | (2) |
|
|
|
750 | (1) |
|
Thread with Member Function |
|
|
751 | (1) |
|
|
|
751 | (1) |
|
|
|
752 | (1) |
|
Retrieving Results from Threads |
|
|
752 | (1) |
|
Copying and Rethrowing Exceptions |
|
|
752 | (3) |
|
Atomic Operations Library |
|
|
755 | (1) |
|
|
|
755 | (2) |
|
|
|
757 | (1) |
|
|
|
758 | (1) |
|
|
|
759 | (1) |
|
|
|
759 | (1) |
|
|
|
760 | (1) |
|
|
|
761 | (1) |
|
|
|
761 | (1) |
|
|
|
761 | (1) |
|
|
|
762 | (1) |
|
Acquiring Multiple Locks at Once |
|
|
762 | (1) |
|
|
|
763 | (1) |
|
Examples Using Mutual Exclusion Objects |
|
|
764 | (1) |
|
Thread-Safe Writing to Streams |
|
|
764 | (1) |
|
|
|
765 | (1) |
|
|
|
766 | (1) |
|
|
|
767 | (3) |
|
|
|
770 | (2) |
|
|
|
772 | (1) |
|
Example: Multithreaded Logger Class |
|
|
772 | (4) |
|
|
|
776 | (1) |
|
Threading Design and Best Practices |
|
|
777 | (1) |
|
|
|
778 | (3) |
|
PART V C++ SOFTWARE ENGINEERING |
|
|
|
Chapter 24 Maximizing Software Engineering Methods |
|
|
781 | (20) |
|
|
|
782 | (1) |
|
Software Life Cycle Models |
|
|
783 | (1) |
|
The Stagewise Model and Waterfall Model |
|
|
783 | (1) |
|
Benefits of the Waterfall Model |
|
|
784 | (1) |
|
Drawbacks of the Waterfall Model |
|
|
785 | (1) |
|
|
|
785 | (1) |
|
Benefits of the Spiral Model |
|
|
786 | (1) |
|
Drawbacks of the Spiral Model |
|
|
787 | (1) |
|
The Rational Unified Process |
|
|
787 | (1) |
|
|
|
788 | (1) |
|
|
|
788 | (1) |
|
|
|
788 | (1) |
|
Software Engineering Methodologies |
|
|
789 | (1) |
|
|
|
789 | (1) |
|
|
|
790 | (1) |
|
|
|
790 | (1) |
|
|
|
790 | (1) |
|
|
|
791 | (1) |
|
|
|
791 | (1) |
|
|
|
792 | (1) |
|
|
|
792 | (3) |
|
|
|
795 | (1) |
|
|
|
796 | (1) |
|
Building Your Own Process and Methodology |
|
|
796 | (1) |
|
|
|
796 | (1) |
|
Bring New Ideas to the Table |
|
|
797 | (1) |
|
Recognize What Works and What Doesn't Work |
|
|
797 | (1) |
|
|
|
797 | (1) |
|
|
|
797 | (2) |
|
|
|
799 | (2) |
|
Chapter 25 Writing Efficient C++ |
|
|
801 | (26) |
|
Overview of Performance and Efficiency |
|
|
802 | (1) |
|
Two Approaches to Efficiency |
|
|
802 | (1) |
|
|
|
802 | (1) |
|
Is C++ an Inefficient Language? |
|
|
802 | (1) |
|
Language-Level Efficiency |
|
|
803 | (1) |
|
Handle Objects Efficiently |
|
|
804 | (1) |
|
|
|
804 | (1) |
|
|
|
805 | (1) |
|
Catch Exceptions by Reference |
|
|
806 | (1) |
|
|
|
806 | (1) |
|
Avoid Creating Temporary Objects |
|
|
806 | (2) |
|
Use Inline Methods and Functions |
|
|
808 | (1) |
|
|
|
808 | (1) |
|
|
|
808 | (1) |
|
|
|
809 | (1) |
|
|
|
809 | (1) |
|
An Object Pool Implementation |
|
|
810 | (2) |
|
|
|
812 | (1) |
|
|
|
813 | (1) |
|
Profiling Example with gprof |
|
|
814 | (1) |
|
|
|
814 | (3) |
|
Profile of the First Attempt |
|
|
817 | (2) |
|
|
|
819 | (2) |
|
Profile of the Second Attempt |
|
|
821 | (1) |
|
Profiling Example with Visual C++ 2013 |
|
|
822 | (1) |
|
Profile of the First Design Attempt |
|
|
822 | (3) |
|
|
|
825 | (2) |
|
Chapter 26 Conquering Debugging |
|
|
827 | (36) |
|
The Fundamental Law of Debugging |
|
|
828 | (1) |
|
|
|
828 | (1) |
|
|
|
828 | (1) |
|
|
|
829 | (1) |
|
|
|
829 | (2) |
|
|
|
831 | (1) |
|
|
|
831 | (4) |
|
|
|
835 | (4) |
|
|
|
839 | (1) |
|
|
|
840 | (1) |
|
|
|
841 | (1) |
|
|
|
842 | (1) |
|
|
|
842 | (1) |
|
Debugging Reproducible Bugs |
|
|
843 | (1) |
|
Debugging Nonreproducible Bugs |
|
|
843 | (1) |
|
|
|
844 | (1) |
|
Debugging Memory Problems |
|
|
844 | (1) |
|
Categories of Memory Errors |
|
|
845 | (3) |
|
Tips for Debugging Memory Errors |
|
|
848 | (1) |
|
Debugging Multithreaded Programs |
|
|
849 | (1) |
|
Debugging Example: Article Citations |
|
|
850 | (1) |
|
Buggy Implementation of an ArticleCitations Class |
|
|
850 | (2) |
|
Testing the ArticleCitations class |
|
|
852 | (9) |
|
Lessons from the ArticleCitations Example |
|
|
861 | (1) |
|
|
|
861 | (2) |
|
APPENDIX A C++ INTERVIEWS |
|
|
863 | (22) |
|
Chapter 1 A Crash Course in C++ and the STL |
|
|
863 | (1) |
|
Chapters 2 and 18: Strings, Localization, and Regular Expressions |
|
|
864 | (1) |
|
Chapter 3 Coding with Style |
|
|
865 | (1) |
|
Chapter 4 Designing Professional C++ Programs |
|
|
866 | (2) |
|
Chapter 5 Designing with Objects |
|
|
868 | (1) |
|
Chapter 6 Designing for Reuse |
|
|
868 | (1) |
|
Chapters 7 and 8 Classes and Objects |
|
|
869 | (3) |
|
Chapter 9 Discovering Inheritance Techniques |
|
|
872 | (1) |
|
Chapter 10 C++ Quirks, Oddities, and Incidentals |
|
|
873 | (2) |
|
Chapters 11 and 21 Templates |
|
|
875 | (1) |
|
Chapter 12 Demystifying C++ I/O |
|
|
875 | (1) |
|
Chapter 13 Handling Errors |
|
|
876 | (1) |
|
Chapter 14 Overloading C++ Operators |
|
|
877 | (1) |
|
Chapters 15, 16, 17, and 20 The Standard Template Library |
|
|
878 | (1) |
|
Chapter 19 Additional Library Utilities |
|
|
878 | (1) |
|
Chapter 22 Memory Management |
|
|
879 | (1) |
|
Chapter 23 Multithreaded Programming with C++ |
|
|
880 | (1) |
|
Chapter 24 Maximizing Software Engineering Methods |
|
|
881 | (1) |
|
Chapter 25 Writing Efficient C++ |
|
|
882 | (1) |
|
Chapter 26 Conquering Debugging |
|
|
882 | (3) |
|
APPENDIX B ANNOTATED BIBLIOGRAPHY |
|
|
885 | (10) |
|
|
|
885 | (4) |
|
|
|
885 | (1) |
|
|
|
886 | (1) |
|
|
|
887 | (1) |
|
|
|
887 | (1) |
|
|
|
888 | (1) |
|
|
|
888 | (1) |
|
|
|
889 | (1) |
|
Unified Modeling Language, Uml |
|
|
889 | (1) |
|
Algorithms and Data Structures |
|
|
890 | (1) |
|
|
|
890 | (1) |
|
|
|
890 | (1) |
|
Software Engineering Methodology |
|
|
891 | (1) |
|
|
|
892 | (1) |
|
|
|
892 | (1) |
|
|
|
893 | (1) |
|
|
|
893 | (1) |
|
|
|
893 | (1) |
|
|
|
893 | (1) |
|
|
|
894 | (1) |
|
Multithreaded Programming |
|
|
894 | (1) |
|
APPENDIX C STANDARD LIBRARY HEADER FILES |
|
|
895 | (8) |
|
|
|
895 | (2) |
|
|
|
897 | (1) |
|
Algorithms, Iterators, and Allocators |
|
|
898 | (1) |
|
|
|
898 | (1) |
|
|
|
899 | (1) |
|
|
|
899 | (1) |
|
|
|
900 | (1) |
|
|
|
901 | (2) |
| Index |
|
903 | |