Monday Jul 14, 2008

Professional C++ Around the World

I have to admit that when I was writing Professional C++ I never imagined this. A couple weeks ago, I received a few copies from my agent, so thought I’d take some pictures of the four different editions I’ve had the pleasure of holding in my hands.

Left to right: US/Canadian/UK edition, Russian edition, Indian (English-language) Edition, Chinese Edition

Bottom to top: US/Canadian/UK edition, Russian edition, Chinese Edition, Indian (English-language) Edition

Thursday May 17, 2007

Reference-Counting Smart Pointers made Surprisingly Simple

Why Do I Need Smart Pointers?

Working with dynamically allocated memory in languages like C++ can be difficult. If you allocate some memory and forget to free it, you get a memory leak. But if you free it more than once you get memory corruption. Not surprisingly, memory management errors are a significant source of bugs in many C++ programs.

Now imagine if you could dynamically allocate memory and use it in your program without ever worrying about freeing it! No, I’m not suggesting you switch to Java. You can obtain this ease-of-use in C++ too, simply by using smart pointers.

[Read More]

Friday May 11, 2007

Class Templates Tutorial

The following tutorial is adapted from Professional C++ Chapter 11, "Writing Generic Code with Templates."

What are Templates?

Most computer languages, including C++, provide support for procedures or functions that allow you to write algorithms that are independent of specific values and can thus be reused for many different values. For example, the sqrt() function in C and C++ calculates the square root of a value supplied by the caller. A square root function that calculated only the square root of one number, like four, would not be particularly useful! The sqrt() function is written in terms of a parameter, which is a stand-in for whatever value the caller passes. Computer scientists say that functions parameterize values.

Templates (called Generics in Java) take the concept a step further to allow you to parameterize on types as well as values. Recall that types in C++ include primitives such as int and double, as well as user-defined classes. With templates, you can write code that is independent not only of the values it will be given, but of the types of those values as well! For example, instead of writing separate queue classes to store ints, Packets, or any other object, you can write one queue class definition that can be used for any of those types.

C++ supports both class templates and function templates. This tutorial will cover the basics of class templates.

[Read More]

Monday Apr 30, 2007

Professional C++

I strongly disliked writing when I was in school. In fact, it probably wouldn’t be too strong to say I hated it. In college I actively avoided classes involving writing assignments. So I sometimes find it hard to believe that not only do I enjoy writing now, I was the lead author of an 838-page book on C++.

My co-author, Scott Kleper, wrote a great post about the process of writing Professional C++, so I won’t dwell on that here. Instead, I’d like to introduce a few of the features that we feel sets Professional C++ apart from other programming books. In future posts I’ll cover some of the particularly interesting aspects of the C++ language and C++ software engineering.

Scott and I believe that teaching C++ programming involves two things: teaching C++ and teaching programming. To that end, we tried to present the C++ syntax and feature-set in the larger context of software engineering and object-oriented methodologies. Specifically,

  • Emphasis on design, including object-oriented design, design themes of abstraction and reuse, design techniques such as smart pointers, and design patterns such as the singleton pattern.

  • Discussion of software engineering methodologies such as extreme programming.

  • Focus on style. We include an entire chapter on C++ style, and call attention to good stylistic practices throughout the book.

  • Inclusion of debugging and testing strategies. We devote over 50 pages to testing and debugging.

  • Discussion of “extra” topics such as writing efficient C++ code, mixing C++ with other languages, and using distributed objects.

In addition, we of course cover all the "usual" C++ stuff: objects, classes, inheritance, memory management, templates, operator overloading, the STL, I/O, exceptions, etc. Watch this space for a tutorial on smart pointers, coming soon!


Nick Solter is a software engineer and author living in Colorado.


« October 2016