eThe The C++ Standard, [stmt.dcl], paragraph 4 [ISO/IEC 14882-2014], states:
...
This compliant solution uses the "construct on first use" idiom to resolve the static initialization order issue. The code for file.h
and file2.c
are unchanged; only the static numWheels
in file1.c
is moved into the body of a function. Consequently the initialization Consequently, the initialization of numWheels
is guaranteed to happen when control flows over the point of declaration, ensuring control over the order. The global object c
is initialized before execution of main()
begins, so by the time get_num_wheels()
is called, c
is guaranteed to have already been dynamically initialized.
...