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
Post a Comment