patterncppCritical
What is the order of evaluation in a member initializer list?
Viewed 0 times
initializerevaluationmemberthelistorderwhat
Problem
I have a constructor that takes some arguments. I had assumed that they were initialized in the order listed, but in one case, it appears they were being initialized in reverse, resulting in an abort. When I reversed the arguments, the program stopped aborting.
Below is an example of the syntax I'm using.
Below is an example of the syntax I'm using.
a_ needs to be initialized before b_ in this case. Can you ensure this order of initialization?class A
{
public:
OtherClass a_;
AnotherClass b_;
A(OtherClass o, string x, int y)
: a_(o)
, b_(a_, x, y) {}
};
Solution
It depends on the order of data member declarations in the class. So
a_ will be the first one, then b_ will be the second one in your example.Context
Stack Overflow Q#1242830, score: 347
Revisions (0)
No revisions yet.