Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



Defensive programming


Overview

To help developers identify potential problems early in development, macros are provided to test for error conditions in functions (asserts) and objects (class invariants).

Casting is one well known source of hard-to-find errors. Casting discusses its use.

[Top]


Testing conditions with asserts and invariants

One method of catching errors early is to identify conditions that should be true at the beginning and end of functions, and raise errors if they are not.

Two mechanisms support this programming style.


Asserts

Two macros are supplied for asserting specific conditions in functions:


Class Invariants

Class invariants are used to test that an object is in a valid state. They are used only in debug builds.

[Top]


Casting

Casts, as in other operating systems, should be used with caution. If a cast seems to be needed, check that this does not reflect a design weakness.

The Symbian platform provides its own macros to encapsulate the C++ cast operators:

Prior to 6.0, GCC did not support the C++ casting operators, and these macros were, therefore, defined as simple C style casts for that compiler. These macros should have been used in preference to using the C++ operators explicitly.

From 6.0 onwards, GCC fully supports the C++ casting operators and the macros have been changed to reflect this in 6.1. This means that from 6.0 onwards, developers should use the native C++ casting operators rather than use the macros.

The more sophisticated C++ dynamic_cast operator should not be used because EPOC does not enable or support Run Time Type Information (RTTI).