Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here, you pass a pointer to an object owned by the list to the constructor of <code>WorldObject</code>:</p> <pre><code>for(std::vector&lt;Point&gt;&amp;pointObject : *inputList) { WorldObject worldObject(&amp;pointObject); tempObjects-&gt;push_back(worldObject); } </code></pre> <p>In <code>WorldObject</code> you store the pointer:</p> <pre><code>//Default Constructor WorldObject::WorldObject(std::vector&lt;Point&gt;* pointList) { float fGeneratedY = (rand() % 20 + 2) / 2.0f;*/ cdt = nullptr; for (Point &amp;point : *pointList) point.setY(HEIGHT); this-&gt;pointList = pointList; } </code></pre> <p>Which means <code>WorldObject::pointList</code> is only valid so long as the <code>std::list</code> which you constructed your <code>WorldObject</code>s from is still around. (After that, the result is undefined -- it could work, it could crash, it could format your hard drive and leak your identity to Texas).</p> <p>If you insist on working with raw pointers, you as programmer are responsible for checking and keeping track of the lifetime of every single pointer. This is error prone and will cause random crashes that you will find difficult to track down.</p> <p>Stop using raw pointers. Instead, if an object owns a resource, store it in a <code>std::unique_ptr&lt;&gt;</code>. If you want the same resource to be shared by multiple objects, use <code>std::shared_ptr</code> and <code>std::weak_ptr</code>, unless the lifetime of all but one of these objects is much, much shorter than the others in a guaranteed way.</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