Errata List for
C++ for Java Programmers

by Timothy A. Budd

Published by Addison-Wesley Longman

329Point 2, run time=>compile time
334comp needs to declare arguments as const (cm)
450Line 7, Slicing occurs only with stack-resident, see footnote. (gk)
4512nd Par. Some earlier compilers will abort on insufficient memory (gk)
460remove void in header for String::StringReference::StringReference (cm)
461Line g = b is wrong, should be g = c (rb)
462should point out that globals will be destructed just before the program exits. (gk)
566See more on include files
579print in child class has wrong text (cm)
579Line -2, should be cast to child pointer (cm)
580Should mention static inner classes (see footnote).
693Sec 6.1.1 Exact size of internal pointer is machine dependent (gk)
694line 6, println should be printf (cm)
696line 4, no & needed in Java code (cm)
696printf at bottom missing newlines (gk)
6102Cat should derive from Animal, not Mammal (cm)
7110List of overloadable operators is not complete, missing pointer dereferencing (gk)
7112rule: const only needed for objects, automatic for primitive types (gk)
7118Missing address operator, this == & right (gk)
7129extra * in ampresand operator, should be ``return this'' (gk)
7130Missing const declarations, see footnote (gk)
8138Line 1, hexadecimal is misspelled (gk)
8138Line 2, 0123 => 123 (no zero) (gk)
8138Line 2, L'01234' => L'\1234' (slash, no zero) (gk)
8138The L modifier can also be applied to strings, to get wide strings (gk)
8139strncopy, strncat - should say "at most n" (cm)
8140Code at bottom, literal => text (twice) (gk)
9154The list proves=>the list provides (line -9)
9159value should be const in find declaration (although const isn't really discussed until chapter 12) (cm)
9161Random numbers, see footnote (gk)
10166should mention that puts adds a newline, fputs doesn't (gk)
10167Error message should go to stderr, not stdout (gk)
10167String for fputs needs newline (gk)
11182Line -15 ``made it into the STL'' should be ``made it into the language''
11186Line 3, iof => of
12196differences between import and include (see footnote)
12201Should mention const_cast, specifically for casting away const (gk)
12201default arguments can only be trailing (gk)
12205midpage, overwritten => overridden (gk)
14233putback code is wrong if EOF is encountered, putting back EOF is complicated(gk)
15239could use find instead of count (gk)
16263see earlier comments on random numbers (gk)


Slicing and Heaps
The assertion that slicing occurs only with stack-resident values is misleading. It is possible, although difficult and rare, to make slicing occur with heap resident values. Consider the following (B is a subclass of A):

	A * anAptr = new A;
	B aBvalue;
	*anAptr = aBvalue; // will get sliced
	anAptr = & aBvalue; // will not get sliced

More on include files
Another implication of the textual inclusion nature of include files should have been mentioned in page 66. That is that an error in an include file (such as a missing semicolon) will frequently only be reported in the next token; that is, in the original file. If you have a misterious error in the first line after a include, the place to look is in the include file itself.

static inner classes (Page 80)
At the time I wrote section 5.7 I was not aware of the fact that the static modifier could be used with inner classes in Java. The static modifier gives the class the same semantics as C++.

Box example on page 130
Geoff Kuenning has noted that this example is missing several const declarations, and has provided a revised version.

Random numbers (pages 161 and 263)
This from Geoff Kuenning:

The randomInteger class suffers from two serious problems. First, there are three different library functions commonly available for generating pseudorandom numbers. Of these, rand() is by far the poorest, and it should always be avoided. Much better pseudorandom numbers are generated by the random() function, which returns a 31-bit pseudorandom integer. The best commonly available generator, however, is drand48(), which also has the advantage of returning double-precision values between zero (inclusive) and 1 (noninclusive). You should always start with drand48() as your preferred generator, and fall back to random() only if drand48() is unavailable.

The other problem with the example code is the use of the modulo (%) operator to reduce the pseudorandom numbers to a small range. I emphasize to my students that they should never use modular arithmetic with pseudorandom numbers! There are two reasons for this:

1. Most pseudorandom numbers are less random in the least significant bits. For example, if you use a max of 16 in Budd's sample class, you may find that your "random" number repeats as often as every 16 values. The most significant bits of the number are much more "random."

2. If your range does not precisely divide the range of the pseudorandom-number generator, there will be a slight bias because some values are more likely than others.

For these reasons, the output of a pseudorandom-number generator should be scaled by division. The easiest way to do it is to convert the result to a double-precision number in the range 0 to 1, and then multiply by the desired range. For example:

    int i = ((double)random() / RAND_MAX) * max;
    int j = max * drand48();
Either of these sequences will generate a pseudorandom integer in the range of 0 to max - 1, inclusive.

Finally, astute students will note that I always refer to pseudorandom, not random, numbers. The values returned by the functions under discussion are not truly random,

differences between import and include
At some point, perhaps in chapter 12, there should have been a discussion of some of the differences between import and include. One of the more subtle differences is transitivity. Suppose file A requires file B. In C++ you can have file A include file B, and if you then include file A you automatically get file B. In Java if the class in file A requires it to import B, then importing A does not automatically mean you get file B. If you need to import the class in B, then it must be done explicitly.

Grateful Acknowledgement

One or more of the errors reported above were found by the following individuals:
Charlie McDowell, Professor of Computer Science, Associate Dean for Undergraduate Affairs, Jack Baskin School of Engineering, University of California, Santa Cruz,
Reinhold Friedrich Burger, University of Waterloo, Canada.
Geoff Kuenning, Computer Science department, Harvey Mudd College.

Where to Write

Send all reports of errors or typos to Professor Budd at

Last Modified: 22 October 2003