(重点是我的)Following code gives correct output, If I declare variables i and j, Like int i, j;class A{ int i, j;public: A(int val) : i(val), j(i + 1) { cout<<i<<endl<<j<<endl; }};But If I declare variable i and j, like int j, i;. then j print garbage value.class A{ int j, i;public: A(int val) : i(val), j(i + 1) { cout<<i<<endl<<j<<endl; }};So, Is it depend on order of declaration of variables? 解决方案 Yes, the data members are always initialized in the order of their declarations, which has nothing to do with the order of the member initializer lists.That means for your 2nd code snippet, j is always initialized before i; but when it's initialized by member initializer i is still not initialized.The complete initialization order for the object is:(emphasis mine) 这篇关于成员初始化程序的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 14:00