Re: Member Initialization Gotcha!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Tommy wrote:
Here is one for the C++ brainaics. :-)

This only took a few hours to figure out after I finally went back to MSDN and saw this warning regarding member initialization:

Warning: The order in which the member initializers are
specified in the constructor does not affect the order in
which the members are constructed; the members are constructed
in the order in which they are declared in the class.

So you would think that two protected const DWORD members shown here would be constructed in its declared order:

class CFooBar {
public
CFooBar(const DWORD d)
: D1(d+10), D2(D1)
{}
protected:
const DWORD D2;
const DWORD D1;
}

Correct?

Well, never mind the above uses D1 to initialize D2, the above is drastically reduce code for illustration. There was a reason for
D2(D1).

What I found is that I had to reverse the order of the member declaration to get it initialize correctly. So with D2 declared first, it would not initialize correctly.

Reversing it like so:

protected:
const DWORD D1;
const DWORD D2;

make it initialize correctly. So if I read the warning right, it would be incorrect because there is indeed a order dependency in order to get the correct member initialization.

Correct?

It showed up when the original constructor:

CFooBar(const DWORD d)
: D1(d+10)
{
D2 = D1;
}

was changed to the above during my port to VC8 code clean up which I better stop doing. :-)

Tommy:

You changed the order of declaration in the class, and the behavior changed (to the one you wanted). How does that contradict the warning?

Personally, I think that initialization that depends on the order of declaration is very fragile, and I would either stick with your original

CFooBar(const DWORD d):
D1(d+10)
{
D2 = D1;
}

or do

CFooBar(const DWORD d):
D1(d+10),
D2(d+10)
{
}

--
David Wilkinson
Visual C++ MVP
.



Relevant Pages

  • Member Initialization Gotcha!
    ... This only took a few hours to figure out after I finally went back to MSDN and saw this warning regarding member initialization: ... So you would think that two protected const DWORD members shown here would be constructed in its declared order: ... Well, never mind the above uses D1 to initialize D2, the above is drastically reduce code for illustration. ... It showed up when the original constructor: ...
    (microsoft.public.vc.language)
  • Re: [PATCH 2/3] user.c: use kmem_cache_zalloc()
    ... struct user_struct *new; ... atomic_t contains just a solitary int member, ... that the *zalloc() is assumed to initialize to zero. ...
    (Linux-Kernel)
  • no default constructor -- bad form?
    ... question (which a C++ question, not a windows question), but the class ... and to make an sense the class must know what the member is. ... But this puts me in the position of having no default constructor, ... I have to initialize x_ to something meaningful when ...
    (comp.lang.cpp)
  • Re: Initializing global variables
    ... Good class writers creates classes do that initialize themselves. ... Hector Santos, Santronics Software, Inc. ... >> automatically initialized member variables for you. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: static variable
    ... >> class MyClass ... >> private: ... >> can I initialize this bool var? ... Little Problem,Guy declared Static Member Public, so you can't initialise ...
    (microsoft.public.vc.mfc)