More C++ Idioms/Attach by Initialization

= Attach by Initialization =

Intent
Attach a user-defined object to a framework before program execution begins.

Also Known As

 * Static-object-with-constructor

Motivation
Certain application programming frameworks, such as GUI frameworks (e.g., Microsoft MFC) and object request brokers (e.g., some CORBA implementations) use their own internal message loops (a.k.a. event loops) to control the entire application. Application programmers may or may not have the freedom to write the application-level  function. Often, the  function is buried deep inside the application framework (e.g,   in case of MFC). Lack of access to  keeps programmers from writing application-specific initialization code before the main event loop begins. The attach-by-initialization idiom is a way of executing application-specific code before the execution of a framework-controlled loop begins.

Solution and Sample Code
In C++, global objects and static objects in the global namespace are initialized before  begins. These objects are also known as objects of static storage duration. This property of objects of static storage duration can be used to attach an object to a system if programmers are not allowed to write their own  function. For instance, consider the following (smallest possible) example using Microsoft Foundation Classes (MFC):

The above example creates a window titled "Hello, World!" and nothing more. The key thing to note here is the global object  of type. The  object is default-initialized before the execution of. As a side-effect of initializing the objects, the constructor of  is also called. The  class is a part of the framework and invokes constructors of several other classes in the framework. During the execution of these constructors, the global object is attached to the framework. The object is later on retrieved by, which is the MFC equivalent of the regular. The  member function is shown just for the sake of completeness and is not an integral part of the idiom. This function is called after  begins execution.

Global and static objects can be initialized in several ways: default constructor, constructors with parameters, assignment from a return value of a function, dynamic initialization, etc.

Caveat

In C++, objects in the same compilation unit are created in order of definition. However, the order of initialization of objects of static storage duration across different compilation units is not well defined. Objects in a namespace are created before any function/variable in that namespace is accessed. This may or may not be before main. Order of destruction is the reverse of initialization order but the initialization order itself is not standardized. Because of this undefined behavior, the static initialization order problem comes up when a constructor of a static object uses another static object that has not been initialized yet. This idiom makes it easy to run into this trap because it depends on an object of static storage duration.

Known Uses

 * Microsoft Foundation Classes (MFC)