Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

C++ Primer 5th Edition, Part 2: How Language Revisions Change Strategy

July 25, 2012

Last week, I noted that textbook authors who revise their books have a natural tendency to add new material in a small number of places rather than integrate it. This tendency tends to make it harder to learn from the book, and it is particularly troublesome when the new material makes the language more abstract — as it has done fairly consistently over the nearly 30 years that I've been associated with C++.

Here is a small example of this phenomenon. The original idea behind adding classes to C was to make C++ classes upward compatible with C structures. In particular, in the early days of copy constructors (1984 or so), copying a class without an explicitly defined copy constructor would simply copy the underlying C structure. Programmers at the time talked about this operation as a "bitwise copy." This behavior caused trouble in cases such as

 
     struct Person {
           String firstname, lastname;
     };
 

There was no standard string library at the time, so I'm using the name String to signify a user-defined class that implements variable-length character strings.

In this early version of C++, this class would be a disaster. The String class presumably includes a pointer to the actual characters that constitute the string, so it will not do simply to copy this pointer as part of copying a Person object. Doing so would cause those characters to be freed twice, once for the original Person object and once for the copy. As a result, programmers in this version of C++ had to supply copy constructors explicitly:

 
     struct Person {
           Person(const Person& p):
		firstname(p.firstname), lastname(p.lastname) { }
                              // and so on
           String firstname, lastname;
     };
 

Any teaching material based on that early version of C++ would have had to teach this programming technique, because even simple abstractions would lead to nasty bugs without the technique. Of course, the difficulty of programming in this way soon led to redefining C++'s default behavior in such cases: Instead of copying the underlying C structure, the C++ compiler would recursively copy the elements of the structure.

Imagine now that you are a textbook author. Throughout your book, your programming examples have dutifully used copy constructors that explicitly copy their classes' data members. Now the language has changed, and all this code is no longer necessary. What do you do?

You could go through the entire book in detail, finding every example that uses one of these now unnecessarily complicated constructors, and either simplify the constructor in question or remove it entirely, as appropriate. Alternatively, you could add a chapter with a title such as Using The New Features that explains that a fair amount of the code in the rest of the book is no longer necessary and can be disregarded.

Although the second of these strategies is much easier for the author, the revised C++ Primer uses the first strategy. There are a number of places in C++11 that make it unnecessary to write code that used to be essential. This fact has resulted in some fairly significant changes to the order in which the book introduces some of its key ideas. Next week, we'll take a look at a concrete example of such a change.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video