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