Symbian3/SDK/Source/GUID-5829247E-2E8E-502F-9051-A59DA8EE71B0.dita
changeset 7 51a74ef9ed63
parent 0 89d6a7a84779
equal deleted inserted replaced
6:43e37759235e 7:51a74ef9ed63
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-5829247E-2E8E-502F-9051-A59DA8EE71B0" xml:lang="en"><title>CBase-derived
       
    13 classes and two-phase construction</title><shortdesc>This document describes the need for two-phase construction and
       
    14 how to use second phase constructors.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    15 <p>When the construction of an object cannot leave (except for out-of-memory
       
    16 for the allocation of the object itself), then it is appropriate to use the
       
    17 conventional C++ constructor, which is automatically invoked by <codeph>new</codeph>. </p>
       
    18 <p>When the construction of an object may leave, then the object must be pushed
       
    19 to the clean-up stack, or a pointer to the object must be stored in an object
       
    20 that would be cleaned up if a leave occurred, <i>before</i> any part of the
       
    21 construction function is invoked that may leave. To allow this to happen,
       
    22 construction steps that can leave, are performed not in the C++ constructor,
       
    23 but in another initialization functions, referred to as a second phase constructor.</p>
       
    24 <p>Thus, the general sequence for two-phase construction is:</p>
       
    25 <ol id="GUID-4EA8A66C-A0FA-58D4-BD86-7FD72029AA17">
       
    26 <li id="GUID-38D6D313-2348-5E1A-95BB-205AF451CC5D"><p>allocate memory for
       
    27 the object (and leave if out of memory) using <codeph>new</codeph> </p> </li>
       
    28 <li id="GUID-1BC50131-75C9-5EFF-9439-030FB35750F9"><p>optionally define a
       
    29 C++ constructor to perform any construction that cannot leave</p> </li>
       
    30 <li id="GUID-DA1E047E-40E6-5B71-BB02-C42C7B3609C8"><p>push a pointer to the
       
    31 object, or store a pointer to it in a class with cleanup support</p> </li>
       
    32 <li id="GUID-6FDA6D4A-9ECD-509D-9D0D-3E7234961657"><p>use the second phase
       
    33 constructor to perform any part of the construction that might leave</p> </li>
       
    34 </ol>
       
    35 <p>Note that:</p>
       
    36 <ul>
       
    37 <li id="GUID-C67CA4EB-480A-5A5B-A4F5-B7206DBD3CFE"><p>The whole sequence is
       
    38 usually encapsulated in static member functions called <codeph>NewL()</codeph>,
       
    39 and <codeph>NewLC()</codeph> (which additionally leaves the created object
       
    40 on the clean-up stack).</p> </li>
       
    41 <li id="GUID-B0AC323F-EDF5-5BEE-B843-A46E5611B1F3"><p>Abstract classes are
       
    42 not intended to be instantiated, and so have no <codeph>NewLC()</codeph> or <codeph>NewL()</codeph>,
       
    43 only a second phase constructor.</p> </li>
       
    44 <li id="GUID-557838D0-10D0-55B3-9848-254065EFDFF4"><p>Step 2 is optional,
       
    45 because all such construction can be performed by the second phase constructor.
       
    46 The C++ constructor is only necessary when the class is immediately derived
       
    47 from a base class whose default C++ constructor cannot be used. In this case,
       
    48 the derived class must call the base class’s constructor with appropriate
       
    49 parameters.</p> </li>
       
    50 <li id="GUID-93707F75-0BB3-563F-B00C-35683DEE007C"><p>It is conventional for
       
    51 the second phase constructor to be called <codeph>ConstructL()</codeph>. </p> </li>
       
    52 </ul>
       
    53 </conbody></concept>