D
davidg
class A
{
public:
A(int x);
...
Amethod();
};
class B
{
public:
B() : a_ref(a), a(42) { }
...
Bmethod() { a_ref.Amethod(); }
...
private:
A &a_ref;
A a;
};
Since the standard states that initializers are executed in the order given in the class definition, this means a_ref is initialized before a, and yet a_ref is trying to reference a.
The reason I ask is that I found the same pattern in the source to the latest Mongodb C++ drivers, and when compiled with GCC it will sometimes wind up leaving a_ref referencing a NULL pointer.
What I mean by that is when B::Bmethod calls a_ref.Amethod(), if I single step from B::Bmethod() into A::Amethod() and inspect "this", it's NULL.
{
public:
A(int x);
...
Amethod();
};
class B
{
public:
B() : a_ref(a), a(42) { }
...
Bmethod() { a_ref.Amethod(); }
...
private:
A &a_ref;
A a;
};
Since the standard states that initializers are executed in the order given in the class definition, this means a_ref is initialized before a, and yet a_ref is trying to reference a.
The reason I ask is that I found the same pattern in the source to the latest Mongodb C++ drivers, and when compiled with GCC it will sometimes wind up leaving a_ref referencing a NULL pointer.
What I mean by that is when B::Bmethod calls a_ref.Amethod(), if I single step from B::Bmethod() into A::Amethod() and inspect "this", it's NULL.