Tools.h++ Manual
104011 Tandem Computers Incorporated 18-9
18
The flag
RWDEBUG
activates a set of PRECONDITION and POSTCONDITION
clauses at the beginning and end of critical functions.
The pre- and postconditions are implemented with “asserts”—a failure will
cause the offending condition to be printed out, along with the file and line
number where it occurred.
RWPRECONDITION
RWPOSTCONDITION
Bertrand Meyer, in his landmark book “Object-oriented Software
Construction”
1
, suggests regarding functions as a “contract” between a caller
and a callee. If the caller agrees to abide by a set of “preconditions”, then the
callee guarantees to return results that satisfy a set of “postconditions”. Here’s
an example with a bounds error in C:
char buff[20];
char j = buff[20]; // Bounds error!
Such bounds error are extremely tough to detect in C, but easy in C++:
RWCString buff(20);
char j = buff[20]; // Detectable bounds error
The reason why is that
operator[]
can be overloaded to perform an explicit
bounds check in the debug version of the library:
char& RWCString::operator[](size_t i)
{
RWPRECONDITION(i < length();
return rep[i];
}
1. Prentice Hall International, ISBN 0-13-629049-3.