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)
This is a discussion on Use of initializer list - is it defined? within the C++ Programming forums, part of the General Programming Boards category; Does anyone think they can answer if the order of initialization in the initialization list is defined? Ie, is this ...
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)
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
It depends on the order in which the members were declared in the class definition.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Then, if I throw it around to something like
Will it be defined behavior?Code:int m_Size; int m_LastIndex; T* m_pArray; public: ArrayList(): m_Size(1), m_pArray(new T[m_Size]), m_LastIndex(0)
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Yes, but risky due to a possible change under maintenance, so I wouldn't do it.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
I see your point. Bad idea.
Thanks.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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
Visual C++ does not, however. And given that it might be a bad idea, since the order of declarations can easily change, I will not pursue it.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
It's your code, you should know whether that will change.
Originally Posted by phantomotap
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
When would you do that and not also need to edit the big three?What if I add or remove some of them sometime?
Originally Posted by phantomotap
I'm just saying I'd rather stay on the safe side. The whole issue is easily avoided in this case by using a constant.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way