June 4, 1998

Be cautious with unsigned integers

Unsigned integers are sometimes unavoidable, like when dealing with raw binary data. A good example is a mail server reading an incoming binary stream. But more often than not, the unsigned specifier is better avoided, since it can be quite dangerous: 1. Unsigned may limit future extensions and can cause

A template’s template argument

Standard C++ supports templates’ template arguments. For instance, a mail server class can store incoming messages in a vector of vector(s) of bytes: vector< vector > vmessages; Please note that the space between the left two angular brackets is mandatory. Otherwise, a >> sequence is parsed as the right shift

Structs as a shorthand for public classes

Traditionally, structs serve as data aggregates. However, in C++ a struct can have constructor(s), destructor and member functions just like a class. The only difference between the two is the default access type: a class has by default private access type to its members and derived objects, whereas a struct

Hiding a base class member function

A derived class may hide a member function declared in its base class by using the same function name, but with a different signature, or list of arguments. Usually, this case suggests a programmer’s mistake, especially if the hidden function is virtual. However, it can also be used as a

Constructors and destructors should be minimal

When designing a class, you should remember that one day it may serve as a base class for other classes. When deriving a class from a base, the base’s destructor and constructor(s) are implicitly copied into the derived class constructor(s) and destructor, respectively. As opposed to ordinary member functions, which