Member Class Initialization

  • Thread starter Mike -- Email Ignored
  • Start date
M

Mike -- Email Ignored

If I have:

class A {...};
class B {...};

class C
{
...
A a;
B b;
};

where all classes have methods that return values
that depend on initialization, and the constructor:

C::C(...numerous args...)
: a(...), b(a.getSomething(),...)
{}

where the value returned by a.getSonething() depends upon
complete initialization of object a,

can I count on object a being initialized in time
to supply the correct value?

Thanks for your help.
Mike.
 
A

Alf P. Steinbach

* Mike -- Email Ignored:
If I have:

class A {...};
class B {...};

class C
{
...
A a;
B b;
};

where all classes have methods that return values
that depend on initialization, and the constructor:

C::C(...numerous args...)
: a(...), b(a.getSomething(),...)
{}

where the value returned by a.getSonething() depends upon
complete initialization of object a,

can I count on object a being initialized in time
to supply the correct value?

In practice yes: initialization is in order of declaration.

But it's brittle. Change the declaration order and you have UB. Perhaps there is
some way of redesigning to avoid the dependency?

In the formal: I think so, but not sure, and I'm not checking because that just
concerns how much leeway the standard's actual (and generally not always
perfect) wording leaves for interpreting it as not providing this guarantee.


Cheers & hth.,

- Alf
 
P

peter koch

* Mike -- Email Ignored:












In practice yes: initialization is in order of declaration.

But it's brittle. Change the declaration order and you have UB. Perhaps there is
some way of redesigning to avoid the dependency?

In the formal: I think so, but not sure, and I'm not checking because that just
concerns how much leeway the standard's actual (and generally not always
perfect) wording leaves for interpreting it as not providing this guarantee.
What? I can't believe that you are not aware of the order of
initialisation, which is a fundamental and sometimes crucial aspect of
C++.
(To the OP: Yes - in your example initialisation is in the order of
declaration. It is always so except for virtual base-classes, where
things get a little bit trickier).

/Peter
 
A

Alf P. Steinbach

* peter koch:
What? I can't believe that you are not aware of the order of
initialisation, which is a fundamental and sometimes crucial aspect of
C++.

He he. Thanks. Yes, I'm aware of it, which is why I wrote "initialization is in
order of declaration", quoted above.

(To the OP: Yes - in your example initialisation is in the order of
declaration.
Yes.


It is always so except for virtual base-classes,

They're initialized first, yes.

where things get a little bit trickier).

Well that's not relevant here, and I didn't even think of that. :)

My comment about the formal concerned whatever guarantees are present for the
evaluation of the memory initializer list arguments, and perhaps other formal
vagueness. Happily, checking, regarding the argument evaluation order there's a
sequence point after the initialization of each base and member, per §12.6.2/3.
But as I wrote there's no real need to consult the formal here since it's
intended to reflect the practice, and any loophole would be unintentional.

I will probably remember that there is a formal guarantee for a few days.


Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top