Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you declare an object of a class <code>myclass</code> like this:</p> <pre><code>myclass ob1(5); </code></pre> <p>it means that you've created it with the <strong>second</strong> constructor which accepts <code>int</code> parameter. In other words, you literally called the <strong>second</strong> constructor. By the way the object is created on stack.</p> <p>When you declare an object of a class <code>myclass</code> like this:</p> <pre><code>myclass ob1; </code></pre> <p>it means that you've created it with the <strong>first</strong> constructor which is parameterless. The right term for parameterless constructor is <strong>default</strong> constructor. In other words, you literally called the <strong>default</strong> (the <strong>first</strong> one) constructor. By the way the object is created on stack too (as in the previous case).</p> <p>When objects are created on stack they are destructed automatically when the object <strong>goes out of scope</strong>. The notion of <strong>scope</strong> applies to the <strong>blocks</strong> of code which include functions/methods/blocks. You can easily distinguish the block by a pair of curly brackets <code>{}</code>. For example:</p> <pre><code>void function() { myclass ob1; } // &lt;--- think of ob1 being destructed at this point </code></pre> <p>the object <code>ob1</code> will go out of scope when function finishes execution and accordingly the destructor for <code>ob1</code> is <strong>automatically called</strong>. Another example:</p> <pre><code>void function() { while(true) { myclass ob1; } // &lt;--- think of ob1 being destructed at this point // ob1 does not exist here! } </code></pre> <p>notice the infinite loop and the object <code>ob1</code> created inside it on every iteration. You can clearly see that curly brackets define a block which can combine multiple statements that constitute each iteration (currently there is only 1 statement). Accordingly, the object <code>ob1</code> will be automatically destructed in the end of each iteration.</p> <p>The notion of scope applies to class members too. For instance, say you have a class <code>wrapper</code>, and you declare <code>myclass</code> as its member (<code>m1</code>) in the following way:</p> <pre><code>class wrapper { public: myclass m1; }; </code></pre> <p>Then lets take one of the previous examples and create an object of <code>wrapper</code> on stack:</p> <pre><code>void function() { wrapper w; } // &lt;--- destructor of w called first, destructor of w.m1 is called after </code></pre> <p>First the execution of the default constructor of <code>w</code> is started, during which <code>w.m1</code> is created (with default constructor in this case because I didn't specify in the constructor of <code>w</code> which constructor of <code>w.m1</code> I'd like to call). Then the constructor of <code>w</code> finishes the execution, and <code>w</code> can be deemed as officially created. The execution of <code>function</code> continues.</p> <p>When <code>function</code> finishes execution, <code>w</code> goes out of scope, and as you remember its destructor is called automatically then. As soon as the destructor of <code>w</code> finishes the execution, the destructor of <code>w.m1</code> is automatically called too. You can think of it as the object <code>w</code> has its own internal scope while it's alive, and a soon as it dies, all of its members go out of scope and should die too.</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