Note that there are some explanatory texts on larger screens.

plurals
  1. POclasses before main and design issues in C++
    primarykey
    data
    text
    <p><strong>PART 1:</strong> I have a very odd situation, where in, I have to get the constructors of several classes called before main() or as soon as it enters main(). There's another sub-system which needs that data ASAP main() of this sub-system starts and more stuff, which make this concept appear more useful.</p> <p>I have two approaches here:</p> <ol> <li><p>Use a global object before main().</p></li> <li><p>use a static class definition, in the first line of main().</p></li> </ol> <p>One more idea, is(since I use gcc) is to use the <code>attribute __constructor__</code> gcc reserved function.</p> <p>What would you suggest?</p> <p><strong>PART 2:</strong> To add further to the issue, I need to decouple the development of those classes.(This is more of a code base design issue.)</p> <p>That is, if someone is writing a <strong>classA</strong> and someone else is writing <strong>classB</strong>, then we would have two separate files with <strong>classA</strong> and <strong>classB</strong>, defined and <em>calling their own constructors within their files/modules</em>.</p> <p>This is a sample code, that illustrates my idea:</p> <pre><code>class Init_myclass { public: Init_myclass(): myvar(10) { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; ":" &lt;&lt; myvar &lt;&lt; std::endl; }; private: int myvar; }; static Init_myclass TestInit; //Initializing object prior to main int main () { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; std::endl; //Use the data populated by the Init_myclass constructor return 0; } </code></pre> <p>In the above code, replace <strong>Init_myclass</strong> with 100s of class definitions like <strong>classA,classB</strong>,etc. </p> <p>The other approach is <em>using a single constructor in a file to call multiple classes constructors defined in other files</em>, from within.</p> <p>Here's the sample code that might help you understand my idea:</p> <pre><code>class InitializeOne { public: InitializeOne(): myvar(10) { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; ":" &lt;&lt; myvar &lt;&lt; std::endl; A bunch of constructors of other classes here(); classA(); classB(); .... }; private: int myvar; }; int main() { static InitializeOne TestInit;//Object after main ... //functions depending on data populated by Initialize constructor } </code></pre> <p>The initialization order of the classes doesn't matter, but they have to be initialized before main or as soon as entering main.</p> <p>Which design is more preferable and why?</p> <p>I hope, I am making my question understandable. If not, do let me know, what information I have missed out or oversimplified/complicated or such.</p> <p>Thank You for your responses and time.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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