views:

60

answers:

1

Does this conform to the standard?

class Foo {
    Bar m_bar;
    Bar * m_woo;
public:
    Foo() : m_bar(42, 123), m_woo(&m_bar) { }
};
+3  A: 

It is correct. What is not correct is dereferencing that pointer before that particular subobject has been fully initialized.

David Rodríguez - dribeas
Dennis Zickefoose
Right @Dennis, but even if the order was the opposite, with the given example it would still be correct, as you can initialize a pointer or reference to the yet uninitialized member. The problem would be if within the initialization list `m_woo` was dereferenced before `m_bar` was initialized.
David Rodríguez - dribeas
No, I understand. I was clarifying in case he does need to dereference `m_woo` for some other member. My intuition says the initialization takes place in the order given by the initializer list, and that intuition is wrong.
Dennis Zickefoose
@Dennis: Nota bene: GCC warns if the initialiser list has a different order from the declarations.
Jon Purdy