REVIEW - Teach Yourself C++


Title:

Teach Yourself C++

Author:

Herbert Schildt

ISBN:

Publisher:

McGraw-Hill Companies (1998)

Pages:

747pp

Reviewer:

Francis Glassborow

Reviewed:

April 1998

Rating:

★☆☆☆☆


Herbert Schildt has worked very hard on this book. He has taken on board many of the criticisms that have been thrown at him for his early books as well as the earlier editions of this one. His

main()
religiously returns an
int
. His header files follow the new C++ form so we have
#include
instead of
#include.
We even have
#include
instead of
#include.
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.

Book cover image courtesy of Open Library.





Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.