As long as the initialization in your example is done in the order you want, this is not for the reason you assume: Initialization occurs in the order of the declaration of the data members in the class definition. . The reason is that the destructor must destroy the members in the reverse order, no matter which constructor was used to create the object. For this, it is necessary to use a constructor-independent way of determining the construction order.
This means that if instead
class myClass { private: ... int m_nDataLength; boost::shared_array<int> m_pData;
someone will change your code to
class myClass { private: ... boost::shared_array<int> m_pData; int m_nDataLength;
then the code will have an error.
My advice:
- Write your constructors so that the initialization order does not matter.
- If you cannot do this (note: for me this has happened less than 5 times in the last decade), make it very clear at the time the data members are declared.
Something like this should do:
class myClass { private: ... int m_nDataLength;
source share