Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, someone pointed out <a href="https://stackoverflow.com/questions/8839943/why-is-it-a-memory-leak-what-could-i-catch-if-i-shall-use-such-things-in-c/8840302#8840302">the memory leak issue</a> to you already.</p> <p>So, you have allocated an array of two animals (<code>ptr = new animal [2];</code>), and stored a pointer to the first one in <code>ptr</code>.</p> <pre><code>+----------+----------+ | speed: 0 | speed: 0 | +----------+----------+ ^ | | ptr </code></pre> <p><sub>(I'm ignoring the <code>position_x</code> and <code>position_y</code>, for the sake of space)</sub></p> <p>Then you set the speed of the first one to 9 (<code>ptr[0].setspeed(9);</code>):</p> <pre><code>+----------+----------+ | speed: 9 | speed: 0 | +----------+----------+ ^ | | ptr </code></pre> <p>Then you do something very weird.</p> <pre><code>ptr++-&gt;setspeed(13); </code></pre> <p>Why you do this, I don't know. Don't do this. I'm not joking. I can figure out what this code means, but I would never write something like this. It only serves to sow confusion.</p> <p>But let's pretend it is a sane thing to do for a while... <code>ptr++</code> increments the pointer, and returns the old value.</p> <pre><code>+----------+----------+ | speed: 9 | speed: 0 | +----------+----------+ ^ ^ | | | | result ptr of ptr++ </code></pre> <p>... and then it sets the speed of the animal pointed by that result to 13.</p> <pre><code>+-----------+----------+ | speed: 13 | speed: 0 | +-----------+----------+ ^ ^ | | | | result ptr of ptr++ </code></pre> <p>Finally, <code>ptr-&gt;print()</code> prints the the animal pointed by <code>ptr</code>, which is the second one.</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