Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simple answer: it depends.</p> <p>When designing your software you should might want to program via the <a href="http://en.wikipedia.org/wiki/Resource_acquisition_is_initialization" rel="noreferrer">RAII</a> principle ("Resource Acquisition is initialization"). This means (among other things) that the object itself is responsible for its resources, and not the caller. Also, you might want to familiarize yourself with <i><a href="http://en.wikipedia.org/wiki/Exception_handling#Exception_safety" rel="noreferrer">exception safety</a></i> (in their different degrees).</p> <p>For example, consider:</p> <pre> void func() { MyFile f("myfile.dat"); doSomething(f); } </pre> <p>If you design the class <code>MyFile</code> in the way, that you can be sure before <code>doSomething(f)</code> that <code>f</code> is initialized, you save a lot of trouble checking for that. Also, if you release the ressources held by <code>f</code> in the destructor, i.e. close the file handle, you are on the safe side and it is easy to use.</p> <p>In this specific case you can use the special properties of constructors:<ul> <li>If you throw an exception from the constructor to its outside world, the object <i>will not be created</i>. This means, the destructor will not be called and the memory will be freed immediately.</li> <li>A constructor <i>must</i> be called. You can not force the user to use any other function (except the destructor), only by convention. So, if you want to force the user to initialize your object, why not via the constructor?</li> <li>If you have any <code>virtual</code> methods, <i>you should not call those</i> from inside the constructor, unless you know what you are doing -- you (or later users) might get surprised why the virtual overriding method is not called. Best not to confuse anyone.</li> </ul></p> <p>A constructor must leave your object in a <i>usable</i> state. And because it is aways wise to <i>make it difficult to use your API wrong</i>, the best thing to do is <i>make it easy to be used correct</i> (<em>sic</em> to Scott Meyers). Doing initialization inside the constructor should be your default strategy -- but there are always exceptions, of course.</p> <p>So: It is a good way to use constructors for initialization. It is not always possible, for example GUI frameworks often need to be constructed, then initialized. But if you design your software completely in this direction, you can save a lot of trouble.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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