Re: Initialization -- One Stage or Two Stage

From: Jonathan Mcdougall (jonathanmcdougall_at_DELyahoo.ca)
Date: 01/04/05


Date: Tue, 4 Jan 2005 16:02:26 -0500


"Brian Folke Seaberg" <bseab1024se@aol.com> a écrit dans le message de news:
20050104145324.21909.00002905@mb-m06.aol.com...
>I have been examining the documentation for a class library written in C++.
>
> It seems that most of the classes in that library use two stage
> initialization.
> Objects are not ready for use after construction. Objects are ready for
> use
> only after post-constructor initialization functions have been executed.
>
> I prefer one stage initialization. I believe that upon completion of
> object
> creation an object should be in a valid state and therefore immediately
> usable.
>
>
> Are there practical reasons to split initialization into two steps?
>
> What does one gain by doing so?

If you don't have the information necessary for the object's construction,
you need to postpone it. One way is to use the heap to construct the object
as necessary but there are circumstances where this is not preferrable
(stack based objects are usually more "efficient"). The only way left is
two-stage initialization.

The problem associated with it is that the object is in an invalid state.
Each function must make sure the object is valid, which requires these
functions to throw, to return an error or set a flag. This is a pain for
the library and the user, but it may be the only way.

A solution would be to provide both approaches, by providing an default
constructor for two-stage intialization plus a create() function and a
constructor with arguments for direct initialization. I think Microsoft
uses that a lot in its MFC.

Jonathan



Relevant Pages

  • Re: How to skip execution during Design Time?
    ... I am fortunate that our system design provides an ordered initialization ... it is design-time during construction, ... only set to run-time mode truly at run-time. ...
    (microsoft.public.dotnet.framework.windowsforms.designtime)
  • Re: best way to handle null objects
    ... object construction, or as part of static member initialization, or should ... of and fulfill prerequisite initialization is poor design. ... incorrectly written program will crash with a NullReferenceException soon ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: another way to shoot yourself in the foot?
    ... initialization of an object Obj_1 of type T1 ... where observable state means result of operations ... How is this seemingly special construction different from just ... If construction turns out to be mostly normal programming, ...
    (comp.lang.ada)
  • Re: Require base class member to be populated by derived classes.
    ... But it's not very different from any of the other half-way solutions that had already been suggested (nor was it clear from your post that your requirement was that the "population" happen during construction). ... In all cases, the general idea is: have some code in the derived class that initializes the members, and have some code in the base class that verifies that initialization. ...
    (microsoft.public.dotnet.languages.csharp)