I've run into the following a few times with initializer lists and I've never been able to explain it well. Can anyone explain why exactly the following fails (I don't have a compiler to catch typos, so bear with me):
class Foo
{
public:
Foo( int i ) : m_i( i ) {} //works with no problem
int getInt() {return m_i;}
~Foo() {}
private:
int m_i;
};
class Bar
{
public:
Bar() :
m_foo( 5 ), //this is ok
m_myInt( m_foo.getInt() ) //runtime error, seg 11
{}
~Bar() {}
private:
Foo m_foo;
int m_myInt;
};
When trying to call member functions of members initialized higher up the initializer list, I get seg faults. I seem to recall this is a known problem (or perhaps somehow by design) but I've never seen it well described. The attached example is contrived with plain old data types, but substitute the Bar::m_myInt
with another object lacking a default (empty) constructor and the issue is more real. Can anyone enlighten me?