Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can think about <em>different</em> solutions. In the loop, no matter the chosen solution, some extra code will be always executed (at best behind the scenes) to prevent the functions to be executed. You could skip in the loop the execution of funX, but it would be extra code to check if funX can be executed, placed differently.</p> <p>Since commonly initialization is done before other task, this is a strange problem, or a misnaming (you should not call it initialization).</p> <p>If you want, the functions can call the initializer by themselves, like</p> <pre><code>if (!initialized) initialize(); </code></pre> <p>Always extra code, and the !initialized will be always false from then on.</p> <p>But the way to avoid to check again and again is to call explicitly the initialization in the correct place, before the game begins to play for real. (Otherwise don't call it initialization).</p> <p>If it were an object, ... you don't need object for this. To "trig" a constructor, you must create the object (at least, in many OO languages) with, say, <code>new MyClass()</code> or "declaring it"; here the call to "new" (that is not a call, but for this speech we can think this way) or the "declaration" is changed into a call to an explicit initializer. Your apparently simple</p> <pre><code>Object a; // ... a.method(); //or Object *a = new Object(); // ... a-&gt;method(); </code></pre> <p>would be</p> <pre><code>init() // ... funA(); </code></pre> <p>Even if this example does not "match" your "need", it should make clear that an explicit initializer is not so tremendously bad and OO languages really are not necessary to "fix" this problem.</p> <p>You can control when you can call those funcs, so you don't need checking at all; it happens in many libraries that require first a call to special init function before you can use them, or "odd" behaviours must be expected.</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