[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [Semi-OT] Advice on whether a C++ book is still adequate

On Thu, Mar 03, 2011 at 10:46:23PM -0600, Ron Johnson wrote:
> I have the dusty book "Teach Yourself C++ 4th Ed" by Al Stevens,
> from... 1995 and wonder that if I go thru it will I screw myself up
> because of new language features.

You will certainly find it confusing.  Most, if not all, code
examples will fail to compile, and you will be learning the
wrong way to do things.  C++ was not standardised (both language and
standard library) until 1999.  This book is pre-standardisation.  The
main issue you'll run into is that the headers were all renamed, and
namespaces were introduced.  For example:

#include <iostream.h>

int main ()
  cout << "Hello world" << endl;

is now

#include <iostream>

int main ()
  std::cout << "Hello world" << std::endl;

(You can use "using std::cout;" to remove the need to use std:: every

But these are the most superficial differences.  You would be able to
use the book and make those corrections as you go through.  But you
would miss out entirely on newer features, most of which you'll
probably want to use (even if you don't realise this at the start):
references, namespaces, templates are good examples.  But this is just
the beginning.  The main power of C++ comes though its standard
library, especially its containers and algorithms.  And then there's
Boost, which is like the standard library, but better, with the
kitchen sink, and on steroids.  And perhaps even more importantly, the
features are just features; the most important things to learn are the
design skills and idioms which will make your code both efficient and

I found this invaluable:
In particular
has some useful recommended texts.  Being a learning by example person,
I found the "official" Stroustrup text dry and uninspiring, unreadable
even.  I've heard good things about Koenig and Moo's Accelerated C++.
I used "Practical C++ Programming" (O'Reilly) which covers the ISO
standard C++, but isn't that amazing, and "Teach yourself C++ for Linux
in 21 days" (SAMS), which is old pre-Standard but comes with lots of
examples.  Both only cover the core language, not the standard library
except superficially.

I bought a copy of Josuttis' The C++ Standard Library, which is an
excellent guide and reference, but it does require learning the
language first.

Also, for later:
Debian got the latest version in unstable just this week.

If you would like to see some examples of modern C++, you could take
a look at schroot.  "apt-get source schroot", or have a browse around
here: http://git.debian.org/?p=buildd-tools/schroot.git;a=tree;f=sbuild;

This makes use of
- templated exceptions
- templated containers (map, list, vector)
- some inheritance (mainly containment and delegation)
- TR1/Boost smartpointers (shared_ptr, weak_ptr) for automatic
  reference-counted memory management, and tuples
- Boost.Regex regular expressions
- Boost.Program_options options parsing
- Boost.Iostreams file descriptor streams to mix streams with basic
  systems programming and file locking [standard iostreams don't
  allow you to create a stream from a file descriptor, let along to
  locking etc.]
- Boost.Filesystem for convenient filesystem functions (creating
  paths recursively etc.)

It also includes a whole bunch of other stuff such as splitting
strings into lists of strings and vice-versa (like perl split and

If there's one thing I'd recommend learning to use, it's
std::tr1::shared_ptr (boost::shared_ptr) from C++03.  This gives you
completely automatic reference-counted memory management.  Whereas
in C or old-style C++, you would do

foo *alloc = malloc(sizeof(foo));
foo *alloc = new foo();

with shared_ptr you do this:

std::tr1::shared_ptr<foo> alloc(new foo());

The advantage is that the former two require a manual free() or delete.
The shared_ptr will free the memory when its destructor is run (when
it goes out of scope).  This is much easier to get correct that manual
management, and is exception-safe.  This means that in practice, you'll
never see a "raw" pointer in good C++.

One of the key things C++ allows that isn't in most of the books are
idioms such as RAII (resource acquisition is initialisation), of which
smartpointers are one example.



  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.

Attachment: signature.asc
Description: Digital signature

Reply to: