Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

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



CBase-derived classes and two-phase construction

When the construction of an object cannot leave (except for out-of-memory for the allocation of the object itself), then it is appropriate to use the conventional C++ constructor, which is automatically invoked by new.

When the construction of an object may leave, then the object must be pushed to the clean-up stack, or a pointer to the object must be stored in an object that would be cleaned up if a leave occurred, before any part of the construction function is invoked that may leave. To allow this to happen, construction steps that can leave, are performed not in the C++ constructor, but in another initialisation functions, referred to as a second phase constructor.

Thus, the general sequence for two-phase construction is:

  1. allocate memory for the object (and leave if out of memory) using new

  2. optionally define a C++ constructor to perform any construction that cannot leave

  3. push a pointer to the object, or store a pointer to it in a class with cleanup support

  4. use the second phase constructor to perform any part of the construction that might leave

Note that: