Microsoft KB Archive/88105

= INFO: Explanation of Two-Phase Construction in MFC =

Article ID: 88105

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft C/C++ Professional Development System 7.0

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51</li></ul>

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

<ul> <li>Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q88105

<div class="notice_section">

<div class="notice_section">

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.

<div class="summary_section">

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.

<div class="moreinformation_section">

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 &quot;cheap&quot; (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.

Additional query words: kbvc100 kbvc150 kbvc151 kbvc152 kbvc200 kbvc210 kbvc400 kbvc500 kbvc600

Keywords: kbinfo kbbitmap KB88105

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.