April 11, 1998

Make sure your switch statements always have a default label

A safe-to-use, long-lasting switch statement should always contain a default label in order to avoid hard-to-detect bugs such as the following: //Month.h fileenum Month { Jan, Feb, Mar, //…etc. Dec};//Month.cpp fileint daysInMonth(Month month) { int result = 0; switch (month) { case Jan: result = 31; break; case Feb: result

How to terminate a program safely?

In standard C, the functions abort() and exit() perform an immediate program termination, regardless of where they are called from. Although this also works in C++, it is usually not a good idea to terminate a program this way, even when a severe error has occurred. The problem with these

Turning a global function into a file-local function

In standard C, a function declared static has an internal linkage; it is accessible only from within the translation unit (source file) in which it is declared, but not from anywhere else. This technique is used to support information hiding as in the following case: //File hidden.cstatic void decipher(FILE *f);//accessible