INFO: Explanation of Two-Phase Construction in MFC

Last reviewed: September 30, 1997
Article ID: Q88105

The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
        - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0
    

SUMMARY

Many of the classes defined in the Microsoft Foundation Classes (MFC) libraries require the programmer to perform two steps to create an object. The CBitmap, CDC, CDialog, and CWnd classes are examples of this class type. This article discusses the advantages of this design decision.

MORE INFORMATION

This two-step process is known as two-phase construction. In the first phase, a C++ constructor creates an object in a sound state, a state that can be destroyed by a destructor function. The second phase, usually performed by the Create() member function, calls into Windows and allocates resources. [For CBitmap objects, the LoadBitmap() member function performs the second phase.]

This design has two main benefits. First, C++ constructors cannot return a value to indicate failure. Operator new throws an exception when no memory is available to construct an object. (An ad hoc mechanism, like an fOK Boolean variable, is one mechanism to indicate an error in construction.)

Second, if constructors are "cheap" (that is, the constructor requires little processor time and does not allocate many resources), an object can be efficiently embedded into another object without a great increase in the cost of construction.

For example, consider the process of embedding a CBitmap object into an object derived from CWnd. If the CWnd-derived object used one-phase construction, its construction would depend on successful construction of the CBitmap object. The programmer would not have any flexibility to deal with any errors as they arose.

However, in the two-phase method, the application can initialize the bitmap any number of different ways while checking for failure. In addition, two- phase embedded objects reduce problems allocating and de-allocating objects, tracking object ownership, and deleting Windows objects appropriately.

Keywords          : MfcMisc kbfasttip
Technology        : kbmfc
Version           : MS- DOS:7.0;WINDOWS:1.0,1.5,1.51,1.52;WINDOWS:1.0,2.0,2.1,4.0,5.0
Platform          : NT WINDOWS
Issue type        : kbinfo


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.