Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should opt for approach 2. if possible.</p> <p>Create one singleton class:</p> <pre><code>class InitializeOnce { public: static InitializeOnce&amp; getInstance() { static InitializeOnce theInstance; return theInstance; } virtual ~InitializeOnce() {} classA&amp; getClassA() { return classA_; } classB&amp; getClassB() { return classB_; } private: classA classA_; classB classB_; InitializeOnce() : classA_() , classB_() { } InitializeOnce(const InitializeOnce&amp; rhs); // Prohibit copying! InitializeOnce&amp; operator=(const InitializeOnce&amp; rhs); // Prohibit assigning! }; </code></pre> <p>... and access it from main once:</p> <pre><code>int main() { static InitializeOnce&amp; theInstance = InitializeOnce::getInstance(); // ... functions depending on data populated by Initialize constructor } </code></pre> <p>In case of approach 1. this would be:</p> <pre><code>static InitializeOnce&amp; theInstance = InitializeOnce::getInstance(); int main() { // ... functions depending on data populated by Initialize constructor } </code></pre> <p><strong>Note</strong><br> Your proposed approaches will result in different behavior, since function local static variables are guaranteed to be instantiated before at least 1st access to them, if you really need accessing it <strong>before</strong> main() is called you have to choose approach 1.</p> <p>IMHO your 3rd idea should be avoided, compiler specific attribute definitions (even if it's gcc), will make the code less portable.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload