Initializing the Object

After the client has successfully created an object of a given class, it must initialize that object. By definition, any new object created using IClassFactory::CreateInstance (or variant or wrapper thereof) is uninitialized. Initialization generally happens through a single call to a member function of the "initialization interface." This interface is usually the one requested by the client in its call to create the object, but this is not required. Before an object is initialized, the only calls that are guaranteed to work on the object (besides the initializing functions themselves) are the IUnknown functions (of any interface) unless otherwise explicitly specified in the definition of an interface. In addition, QueryInterface is only guaranteed to work for IUnknown and any initialization interface, but not guaranteed for a non-initialization interface.

Some objects will not require initialization before they are function through all of their interfaces. Those that do require initialization will define, either explicitly through documentation of the object or implicitly through the scenarios in which the object is used, which member of which interface can be used for initialization.

For example, objects that can serialize their persistent data to a file will implement the IPersistFile interface (see "Persistent Storage Interfaces for Objects" in Chapter 8). The function IPersistFile::Load, which instructs the object to load its data from a file, is the initialization function and IPersistFile is the initialization interface. Other examples are objects that can serialize to storages or streams, where the objects implement the initialization interfaces IPersistStorage or IPersistStream, respectively (again, see Chapter8). The Load functions in these interfaces are initialization functions as is IPersistStorage::InitNew, which initializes a new object with storage instead of loading a previously saved version.