Member Initialization Gotcha!

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



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. :-)

Go Figure!

What say you?

--
.



Relevant Pages

  • 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: 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. ...
    (microsoft.public.vc.language)
  • Re: public static final - compilation error
    ... > is it possible to initialize a public static final member of a class in ... > the class' constructor? ... Prev by Date: ...
    (comp.lang.java.help)
  • Re: Best practice and is there a difference?
    ... classes and not classes where the constructor takes arguments. ... private String string1 = String.Empty; ... I go ahead and initialize them where it is ... In the above case, while I create the list at the member declaration, I ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Arrays!
    ... > innitialize a two dimensional array in my constructor of my class. ... you declare and initialize a local variable named ... There is no syntax in C++ to initialize an array that is a member ...
    (microsoft.public.vc.language)