Why must initializer list order match member declaration order?

后端 未结 3 1184
[愿得一人]
[愿得一人] 2020-12-03 20:46

Why does gcc throw a hissy fit if the initializer list order doesn\'t match variable order in the class?

class myClass
{
public:
   int A;
   int B;
   myCla         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-03 21:41

    The warning is trying to prevent situations where you might be relying on the wrong ordering of the data members. Say you think B is initialized before A, and then you do something like this:

    myClass::myClass() :
    B(42), A(B) {}
    

    Here, you have undefined behaviour because you are reading from an uninitialized B.

提交回复
热议问题