Archive for the 'C++' Category

Default Constructor vs. Empty Constructor

December 16th, 2006

While coding away new classes for your C++ project, do you have the habit of just dropping in a contructor definition, just in case you may in the future need to do some initialization at construction? A while ago, I had wondered if there would be any difference at compile time between having an empty constructor and having no constructor at all (and thus using the default constructor). I did a quick investigation by writing a simple test application and looking at the disassembly. It turned out that there is actually a difference (in a debug build). Read the rest of this entry »


Opt for Pre-incrementing Iterators

August 26th, 2006

There’s a slight performance advantage in using pre-increment operators versus post-increment operators. In setting up loops that use iterators, you should opt for using pre-increments:

for (list<string>::const_iterator it = tokens.begin();
    it != tokens.end();
    ++it) { // Don't use it++
    ...
}

The reason comes to light when you think about how both operators would typically be implemented. Read the rest of this entry »


Don’t Hand-count Characters

July 15th, 2006

A common mistake that most programmers do is to hard-code hand-counted lengths of string literals. For example:

if (!strncmp(str, "MAGIC_PREFIX_", 13)) {
    ...
}

Even if the string literal "MAGIC_PREFIX_" is not due to change in a life time, it makes the code more prone to human error and harder to read when the length of the string is hand-counted and hard-coded as a separate entity (13 in this case). Read the rest of this entry »