c++ - Initialization during construction? -


i wrote following code:

struct a{     int a;     int b;     a(int c): a(c), b(a){ } };   int main() {     b(10); } 

now, i'm not sure initializing b a a(c), b(a). ok or may cause ub?

yes, okay. members initialized in order declared in class. note order of initializers doesn't matter, work (but not practice):

struct a{     int a;     int b;     a(int c): b(a), a(c) { } }; 

but not work:

struct a{     int b;     int a;     a(int c) : a(c), b(a) { }  }; 

some compilers can give warning if initializer order doesn't match declaration order.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -