Does anyone think they can answer if the order of initialization in the initialization list is defined? Ie, is this safe?
Code:ArrayList(): m_Size(1), m_pArray(new T[m_Size]), m_LastIndex(0)
It depends on the order in which the members were declared in the class definition.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, but risky due to a possible change under maintenance, so I wouldn't do it.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It may not be a best idea, but the compiler might at least be able to warn, if the initialization doesn't occur in the good order (e.g that you initialize m_lastIndex after m_pArray here).
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
The initialization ALWAYS occurs in the order the members are declared. If the initializer list is in a different order, the compiler will probably warn, but it doesn't change the initialization order.
If the members were initialized in the order given in the list, then two constructors could exist which initialize the members in two different orders. Upon destruction, the members would then potentially be destructed in the wrong order.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Yes, I know that. May-be I worded it badly, but in the example, the initializer list is in an incorrect order and you might expect a warning.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I.e., gcc gives you such:
Code:temp.cpp: In constructor `ArrayList::ArrayList()': temp.cpp:5: warning: `ArrayList::m_Size' will be initialized after temp.cpp:4: warning: `int*ArrayList::m_pArray' temp.cpp:12: warning: when initialized here temp.cpp:4: warning: `ArrayList::m_pArray' will be initialized after temp.cpp:3: warning: `int ArrayList::m_LastIndex' temp.cpp:12: warning: when initialized here
It's your code, you should know whether that will change.
I most certainly would, but I don't want to introduce dependencies on variable declaration order.
What if I add or remove some of them sometime? Shall I put in comments saying: this variable MUST be declared at this very exact position, and ABSOLUTELY before this and after this? Annoying.
When would you do that and not also need to edit the big three?What if I add or remove some of them sometime?
When you re-arrange the declarations for cosmetic purposes, or maybe with the hope of better packing.Originally Posted by whiteflags
Anyway, moving the new[] to the constructor body would be consistent with what one would do if more than one new or new[] was to be used in the constructor.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)