I have:
class Foo;
class Bar {
Foo foo;
Bar(): foo(foo) {};
}
Bar bar;
At this point, is
bar.foo // <--- how is this initialized?
[This question arose from a buggy ref-counted pointer implemntation; I could have sworn that I ensured each pointer was pointing at something non-null; but I ended up with a pointer that pointed at something NULL.]
foois fully initialized once you’ve entered the body of the constructor (that’s the guaranteed general case; specifically once it has finished initializing in the initialize list.)In your case, you are copy-constructing from a non-constructed object. This results in undefined behavior, per §12.7/1 (thank you, gf):
In fact, it gives this example:
Note, the compiler is not required to give a diagnosis of undefined behavior, per §1.4/1. While I think we all agree it would be nice, it simply isn’t something the compiler implementers need to worry about.
Charles points out a loophole of sorts. If
Barhas static storage and ifFoois a POD type, then it will be initialized when this code runs. Static-stored variables are zero-initialized before an other initialization runs.This means whatever
Foois, as long as it doesn’t need a constructor to be run to be initialized (i.e., be POD) it’s members will be zero-initialized. Essentially, you’ll be copying a zero-initialized object.In general though, such code is to be avoided. 🙂