Let's say I have this constructor :
MyClass::MyClass()
: m_foo(new Foo())
, m_bar(new Bar(m_foo))
{
}
Is it safe/legit to use a member like this to initialize other members ?
Thanks
Let's say I have this constructor :
MyClass::MyClass()
: m_foo(new Foo())
, m_bar(new Bar(m_foo))
{
}
Is it safe/legit to use a member like this to initialize other members ?
Thanks
It depends on the order in which m_foo
and m_bar
appear in the class definition. Since data members are initialized in order of definition, the code above is safe only if m_foo
appears before m_bar
.
Compilers usually warn about order mismatches between the constructor initialization list and class definition.