[Home Page] - [Reviews Main] | |
|
Teach Yourself C++ 3ed by Herbert Schildt Not Recommended |
| ISBN: 0 07 882392 7 Publisher: McGraw-Hill Pages: 746pp Price: £22-99 |
| Categories: beginner's c++ |
| Reviewed by Francis Glassborow in C Vu 10-2 (Jan 1998) |
main() religiously returns an int. His
header files follow the new C++ form so we have #include
<iostream> instead of #include <iostream.h>.
We even have #include <cstdio> instead of
#include <stdio.h>.
In his dedication he thanks four people for sharing their knowledge,
advice and expertise. I guess it is the only time that Al Stevens will
share credit with Bjarne Stroustrup, Steve Clamage and P J Plauger (Al
almost caused apoplexy among the members of J16 with a couple of his
recent columns in Dr Dobbs Journal.)
After a quick overview of C++ Schildt dives into the mysteries of
writing classes in chapter 2. So you can guess that the first problem
is that the book assumes you already know C. You would be right.
To exhibit the second problem I am going to quote verbatim from pages
220 and 221 where he provides an assignment operator for a string class
he dabbles with by way of an example of a class. The class has two
data members p which is a char * to
handle a dynamic array to hold the string and len which
holds the current capacity of the string object. Now read on:
strtype & strtype::operator=(strtype &ob){
// see if more memory is needed
if (len > ob.len) {
// need to allocate more memory
delete [] p;
p = new char[ob.len];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
}
len = ob.len;
strcpy(p, ob.p);
return *this;
}
When you have recovered from the shock of reading this take a deep
breath and read his commentary:
As you can see, the overloaded assignment operator prevents p
from being overwritten. It first checks to see if the object on the
left has allocated enough memory to hold the string that is being
assigned to it. If it hasn't, that memory is freed and another portion
is allocated. Then the string is copied to that memory and the length
is copied into len.
Notice two other important features about the operator=() function.
First, it takes a reference parameter. This prevents a copy of the
object on the right side of the assignment from being made. As you
know from previous chapters, when a copy of an object is made when
passed to a function, that copy is destroyed when the function
terminates. In this case, destroying the copy would call the
destructor function, which would free p.
However this is the same p still needed by the object used as an
argument. Using a reference parameter prevents this problem.
The second important feature of the operator=() function is that it
returns a reference, not an object. The reason for this is the same as
the reason ituses a reference parameter. When a function returns an
object, a temporary object is created that is destroyed after the
return is complete. However, this means that the temporary object's
destructor will be called, causing p to be freed, but p (and the memory
it points to) is still needed by the object being assigned a value.
Therefore, by returning a reference, you prevent a temporary object
being created.
I find it impossible to believe that anyone who has the slightest idea
how C++ works could write the above. The source code would be an
excellent student exercise in detecting serious flaws. The commentary
is at best confused. The writer seems to have no idea that any class
that needs a copy assignment also needs a copy constructor. He
completely ignores the self-assignment problem. He does not seem to
know that new throws an exception if there is an
allocation failure. Even if he is writing code for a
new(nothrow) self respecting C++ programmers do not call
exit() and certainly do not do so in a function such as this
one. There are also some deep problems such as his use of a reference
parameter when a const reference will cause fewer
surprises.
I wanted to like this book if not least because the author seems to have responded to earlier criticism but in reality it is clearly superficial and despite his dedication he has learnt very little from the experts he credits. I suppose I could have expected nothing more from an author who allows his publisher to place in large (extra large for the author's name) bold type the statement:
When you need solid answers, fast, turn to Herbert Schildt, the recognized authority on programming. Sorry, but he is not recognised as an expert by me nor by any C or C++ expert that I know. It would do him an immense amount of good to humbly read Bjarne Stroustrup's The C++ Programming Language , the third edition for preference, but any one would improve his programming. I would not expect an experienced C programmer to perpetrate this kind of C++ after a one-week conversion course from a competent trainer.
To link to this review, please use the URL: http://www.accu.org/bookreviews/public/reviews/t/t001453.htm
Copyright © The Association of C & C++ Users 1998-2000. All rights reserved.