constructor initializing members explicitly

constructor initializing members explicitly

Post by Bori » Tue, 30 May 2000 04:00:00



class Foo

  int A;
  int B;

  Foo(int X) : B(A), A(X) { };

Quote:};

I read in the VC++ help that members are constructed in the order in which
they are declared in the class. Is this VC++-only or guaranteed by C++? Does
the above explicit members initializing by the constructor work? That is may
I use a just initialized member (A) for initializing another member (B)?

Boris

 
 
 

constructor initializing members explicitly

Post by Jeffrey Ri » Tue, 30 May 2000 04:00:00


Quote:> class Foo

>   int A;
>   int B;

>   Foo(int X) : B(A), A(X) { };
> };

> I read in the VC++ help that members are constructed in the order in which
> they are declared in the class. Is this VC++-only or guaranteed by C++?

C++ standard

Quote:> Does
> the above explicit members initializing by the constructor work? That is may
> I use a just initialized member (A) for initializing another member (B)?

You may but it seems to be pontentially dangerous code to me.  It is better for
the construction of each member of a class to not depend on the order of
construction.  However, the above code should work

Quote:

> Boris

Jeffrey

 
 
 

constructor initializing members explicitly

Post by Bori » Tue, 30 May 2000 04:00:00




Quote:

> [...]
> You may but it seems to be pontentially dangerous code to me.  It is
better for
> the construction of each member of a class to not depend on the order of
> construction.  However, the above code should work

Wow, this was a fast answer! :-)

Thank you,
Boris

 
 
 

constructor initializing members explicitly

Post by grzegor » Tue, 30 May 2000 04:00:00



> class Foo

>   int A;
>   int B;

>   Foo(int X) : B(A), A(X) { };
> };

> I read in the VC++ help that members are constructed in the order in which
> they are declared in the class. Is this VC++-only or guaranteed by C++? Does
> the above explicit members initializing by the constructor work? That is may
> I use a just initialized member (A) for initializing another member (B)?

> Boris

 If you mean  Foo(int X) : A(X),B(A) {}; then yes.

--
  grzegorz

 
 
 

constructor initializing members explicitly

Post by grzegor » Tue, 30 May 2000 04:00:00





> > > class Foo

> > >   int A;
> > >   int B;

> > >   Foo(int X) : B(A), A(X) { };
> > > };

> > > I read in the VC++ help that members are constructed in the order in
> which
> > > they are declared in the class. Is this VC++-only or guaranteed by C++?
> Does
> > > the above explicit members initializing by the constructor work? That is
> may
> > > I use a just initialized member (A) for initializing another member (B)?

> > > Boris

> >  If you mean  Foo(int X) : A(X),B(A) {}; then yes.

> I thought members are constructed in the order in which they are declared in
> the class? As I wrote "int A;" first and then "int B;" my initialization
> list should work, too?

> Boris

Yes , order of declaration is important , not order of initializers.
So your code should work, but it looked so bad, i couldn't stay
away from fixing it.
And mixing initializers like this doesn't make any sense.

--
  grzegorz

 
 
 

constructor initializing members explicitly

Post by Bori » Wed, 31 May 2000 04:00:00




> > class Foo

> >   int A;
> >   int B;

> >   Foo(int X) : B(A), A(X) { };
> > };

> > I read in the VC++ help that members are constructed in the order in
which
> > they are declared in the class. Is this VC++-only or guaranteed by C++?
Does
> > the above explicit members initializing by the constructor work? That is
may
> > I use a just initialized member (A) for initializing another member (B)?

> > Boris

>  If you mean  Foo(int X) : A(X),B(A) {}; then yes.

I thought members are constructed in the order in which they are declared in
the class? As I wrote "int A;" first and then "int B;" my initialization
list should work, too?

Boris

 
 
 

constructor initializing members explicitly

Post by fungu » Wed, 31 May 2000 04:00:00



> Yes , order of declaration is important , not order of initializers.

FWIW, I just tried it the other way round and it all went to hell.
A ended up with a bad value in it.

Quote:> So your code should work, but it looked so bad, i couldn't stay
> away from fixing it.
> And mixing initializers like this doesn't make any sense.

I expected at least a warning from the compiler, but
apparently Microsoft don't see this sort of thing as
important. Oh, well...

--
<\___/>
/ O O \
\_____/  FTB.

 
 
 

constructor initializing members explicitly

Post by Bori » Wed, 31 May 2000 04:00:00




> [...]
> > I thought members are constructed in the order in which they are
declared in
> > the class? As I wrote "int A;" first and then "int B;" my initialization
> > list should work, too?

> Yes , order of declaration is important , not order of initializers.
> So your code should work, but it looked so bad, i couldn't stay
> away from fixing it.

Hehe.

Quote:> And mixing initializers like this doesn't make any sense.

This was only an example. My real problem is that I have a struct class
member which should be used as a first element in a list (also class
member). I want to initialize the struct first and then initialize the list
with the just initialized struct. Something like this:

Foo::Foo(int _a, int _b) : astruct(_a, _b), alist(1, astruct)
{ }

Boris