...
This compliant solution uses the "construct on first use" idiom to resolve the static initialization order issue. Because the The code for file.h
and file2.c
are unchanged; only the static num_wheels
object was in file1.c
is moved into the body of a function, its initialization is . Consequently the initialization of num_wheels
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.
...