ProgrammingHardProduct-company placement · C++

A global object g1 defined in file1.cpp uses the global object g2 defined in file2.cpp inside g1's constructor. Both have dynamic initialisation. What does the C++ standard guarantee?

  1. A. g2 is always initialised first because it is used by g1
  2. B. Globals are initialised in alphabetical order of their file names
  3. C. The linker reorders initialisation to satisfy dependencies
  4. D. Nothing about which one is initialised first, so g1 may see g2 before it is constructed

Correct answer: D. Nothing about which one is initialised first, so g1 may see g2 before it is constructed

Initialisation order is defined within one translation unit (definition order) but unspecified across translation units: the 'static initialisation order fiasco'. A function-local static (construct on first use) avoids it.

More OOP with C++ and Java questions

Exams that ask this topic