Search code examples
c#c++initializationinitialization-order

Why C++ forces initialization of member variables to be in the order of the declaration


I know that in C++ the declaration of members in the class header defines the initialization order. Can you tell me why C++ choose this design? Are there any benefits to force the initialize order instead of following the initializer list?


Solution

  • Constructors could be overloaded while destructor can't. If data members could be initialized in different order for different constructors, then the destructor can't guarantee to perform destruction on data members in the reverse order of their construction (for objects constructed by different constructors).