Re: C++ design question

From: H. S. Lahman (h.lahman_at_verizon.net)
Date: 09/29/04


Date: Wed, 29 Sep 2004 20:05:26 GMT

Responding to Kurth...

It was a nice explanation, but I was already aware of all that. [Or at
least I was a decade+ ago when I wrote my last C++. B-)]

> The initializers are called in the order they are declared rather then the
> order listed in the initialization list.

Which is a big part of the problem. There is a Catch-22 because
references must be in the initializer list but the constructor argument
itself may not be initialized when it is processed because of that
ordering. The problem here really highlights the difference between
instantiation and initialization, which is mostly hidden by the OOPL
syntax until one gets into an arcane situation like the OP's
self-referencing.

The OP's safety issue lay in initializing the reference object itself,
not the instantiation of the embedded reference. The solution is to use
the default constructor (which doesn't even have to be in the
initializer list) to do that instantiation while doing the actual
initialization of the reference object in the body via setters as I
suggested.

*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hsl@pathfindermda.com
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindermda.com
blog (under constr): http://pathfinderpeople.blogs.com/hslahman
(888)-OOA-PATH



Relevant Pages

  • Re: C++ design question
    ... >The OP's safety issue lay in initializing the reference object itself, ... >not the instantiation of the embedded reference. ... >initialization of the reference object in the body via setters as I ... If you try to compile the example you gave, ...
    (comp.object)
  • Re: Beginner level VB.NET question
    ... were a C# programmer. ... object to nothing somewhere in the code, but not realizing it is the same ... NullReferenceException;(Am I checking whether my reference Object is ... ' Initialization and other good stuff ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Beginner level VB.NET question
    ... were a C# programmer. ... object to nothing somewhere in the code, but not realizing it is the same ... NullReferenceException;(Am I checking whether my reference Object is ... ' Initialization and other good stuff ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Message Builder vs. a Build Method?
    ... Builder class would encapsulate all the complex algorithms for making ... OTOH, as Daniel T. suggests, sometimes the initialization requires unique processing for initialization that is clearly intrinsic to the object itself. ... Constructors tend to be fragile and it is difficult to manage errors when they occur in a constructor scope, so it is usually a good idea to keep the processing in constructors as simple as possible. ... When the initialization of attribute data requires complex processing AND it seems like is intrinsic ot the object, that justifies having a separate initialization method that is invoked immediately after the constructor. ...
    (comp.object)
  • Re: Cant get static SolidBrush to initialize... what am I missing
    ... The order of instantiation problem is amazing given the way I have it setup. ... At the file scope I attempted to instantiate the Gdipm ... Any statements at namespace/file scope that define variables are executed ... the order of initialization across files is unspecified. ...
    (microsoft.public.vc.language)