Tour of C++: Second chapter posted

Save to:
Instapaper Pocket Readability

This 2nd chapter of my Tour of C++ introduces the basic C++ abstraction mechanisms. If you have a 1990s view of C++, you are in for a few surprises: the integrated set of abstraction mechanisms in C++11 allows for simpler and more powerful abstractions than previously. For starters, pointers basically disappear from sight (hiding inside resource handles). Naturally, this is achieved without loss of performance. Enjoy!

Constructive comments would be most welcome.

Add a Comment

You must sign in or register to add a comment.

Comments (18)

0 0

Arthur said on Dec 3, 2012 12:26 AM:

I believe that there is a "typo" in chapter 3, p.78, third sentence after code example:

"The most embarrassing part is that **res** is never used again after the copy."

The 'res' word is nowhere to be found in a code, and I believe it refers to 'r' variable.
0 0

Sebastian G. said on Dec 4, 2012 05:24 AM:

Nice Tour!

@Arthur: I think by 'res' he meant to refer to the temporary objects that operator+ returns.
0 0

Bjarne Stroustrup said on Dec 4, 2012 09:01 AM:

"He" did indeed. Thanks for pointing out the problem. I have already clarified the text.
0 0

Boris said on Dec 4, 2012 01:37 PM:

Hi! I'm not a native english speaker, but I think that the 2nd "technique" in the 4th sentence on p. 66 ("The technique of acquiring ...") is probably unwanted, just as the semicolon in "naked new operations;". Thanks for the early look into your new book!
0 0

Bjarne Stroustrup said on Dec 4, 2012 02:21 PM:

Thanks. The second "technique" is definitely redundant. My publisher was quite nervous about letting chapters out before copy editing and proof reading.
0 0

Boris said on Dec 4, 2012 11:54 PM:

I think there are two minor issues with the source code on p. 82: In the 2nd code block, the comment about the
is wrong, and in the code block below, there is an additional ")" in the header of the for-loop.
0 0

Bjarne Stroustrup said on Dec 5, 2012 08:17 AM:

0 0

Naveen Mankotia said on Dec 5, 2012 10:34 AM:

In Vector examples on pages 51, 53, 54, 55 initialization list uses curly brackets like Vector()::Vector(int s) : elem{new double[s]}, sz{s} {} instead of round brackets
0 0

Bjarne Stroustrup said on Dec 5, 2012 11:58 AM:

Yes. That's deliberate - using uniform initialization (See the first Tour chapter)
0 0

Naveen Mankotia said on Dec 5, 2012 04:56 PM:

0 0

Boris said on Dec 5, 2012 09:54 PM:

I own a printed version of "The C++ Programming Language" (a german translation from 2000). When I look at its 2nd chapter I can see that you consistently avoided using the "private" keyword in class definitions and always put the private members at the top. In the two PDFs of your new book, the style varies among class definitions. I wondered whether this might confuse a reader who is just beginning with C++ or was it a deliberate decision to show possible variations in style?
0 0

Sebastian G. said on Dec 6, 2012 12:30 PM:

3.4.3 Function Objects [tour2.functionobjects] (page 83)

The class template Less_than stores a reference instead of a value. In the following example code, temporary objects are used to initialize the const reference member and the temporary objects cease to exist which makes the references dangle. Later, undefined behaviour is invoked by use of the Less_than objects.
0 0

tomaszmi said on Dec 6, 2012 01:10 PM:

Congrats! A very promising book. A typo in section 3.3.2, page 79, line #4 (numbering from 1): "rght-hand side". Shouldn't it be "right-hand side"?
0 0

Boris said on Dec 7, 2012 01:52 AM:

There's a wrong variable name in the definition of class complex on p. 63:
void imag(double d) { im=i; }

A few "typo" kind of issues:
On p. 74 after the 2nd code block, in the sentence "Note that there are ...", function "user()" is spelled "user" once.
In the last paragraph on p. 79: "By defining constructors ... and a destructors, ..."
The last sentence of the 1st paragraph of 3.3.4, p.80: "... to eliminate to default definitions ..."

Also, although these may be purely matters of taste:
You might consider not starting the first two sentences of 3.1 both with "This chapter" and the last sentence before 3.2.1 reads somewhat awkward, is there a "which" missing?
0 0

Boris said on Dec 7, 2012 04:05 AM:

I think that in the first code block on p. 80, "vec" should be declared as a "Vector". The 2nd sentence in the first paragraph of 3.4.2 points to 3.4.1 and 3.4.2 which might not be intended. The 2nd last paragraph on p. 84 ("Function objects that is ...") contains a few typos. And in the 2nd text paragraph on p. 86, "The ellipses, ..." may be intended to be a singular.
0 0

Bjarne Stroustrup said on Dec 8, 2012 08:39 AM:

Thanks! These comments are most helpful. Any comments on Chapter 2? Any general comments on the approach or level of discussion?
0 0

Boris said on Dec 9, 2012 08:55 AM:

While reading Chapter 2, I stumbled across the following things:
*** The 2nd paragraph of 2.1 is the same as the 2nd paragraph of 3.1.
*** In the 3rd paragraph of 2.1, the STL's "unique_ptr" is spelled "unique_pointer".
*** On p. 44, the "<>" notation for template argument types suddenly appears in variable declarations without any explanation, this might confuse a reader who hasn't seen this before.
*** The sentence on p. 50 about the heap/free store/dynamic memory (middle of the page) can be found almost identically on p. 65 (also at the middle of the page).
*** In the 1st paragraph on p. 52 it is claimed that "a Vector object can have a different number of elements at different times." I don't think that's true for the "Vector" as it is defined at that point, because the size can only be set in the constructor and not changed.
*** In the last sentence before 2.4.1, a "the" is missing (top of p. 55).
*** In the last sentence of, the "static_cast" should probably be a "static_assert" and I think there's a "try" missing or maybe a "to" too much in that sentence.
*** Thanks again for making this available here. I'm having fun reading it and look forward to the next parts!
0 0

Bjarne Stroustrup said on Dec 9, 2012 12:30 PM:

Thanks! (again). Please note that the posted text has still to be copy edited and proof read. Some repetition is deliberate: My experience is that these chapters often get read separately and/or out of order.

I think that the most important point about these chapters is not any one detail, but that they present a 201d view of C++ rather than a 198d's view.