INFO: Explanation of Two-Phase Construction in MFC (88105)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft C/C++ for MS-DOS 7.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0
    • Microsoft Visual C++ .NET (2002)
    • Microsoft Visual C++ .NET (2003)

This article was previously published under Q88105
NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

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.

Modification Type:MajorLast Reviewed:9/23/2003
Keywords:kbBitmap kbinfo KB88105 kbAudDeveloper