Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will reply because predecessors answered only to the question from topic while you are asking more questions and I will also give some advice. In the remainder of this post we will assume that if <code>Points</code> do allocate any dynamic memory, the memory is returned properly when <code>Points</code> are deleted.</p> <blockquote> <p>Class Points has only two instance vars that are all ints but the number of points will only be known when I read this in from the file, so I guess I have to make Points dynamically with new which means I have to destroy them later.</p> </blockquote> <p>This comes with contradiction to what you actually do here</p> <pre><code>class Grid{ public: // initialize vector with 3 points with val 0. Grid(vector&lt;Points&gt; v) : vector&lt;Points&gt;(3, 0) {}; // is this right private: vector&lt;Points&gt; v_points; }; </code></pre> <p>because you create vector without <code>new</code>. However this might be OK if we assume that you first get number of <code>Points</code> and then you are about to create a Grid. <code>std::vector</code> is not C array and might be easily resized, assigned and gives much more flexibility. You do not have to create it on heap because you are afraid about size: vector elements are <a href="https://stackoverflow.com/a/8036528/1141471">always</a> created on heap, it is just a vector itself that is (if it is) on stack. And this is often exactly what we want (see i.e. <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="nofollow noreferrer">RAII</a>). The proper way to initialize a vector would be then</p> <pre><code>class Grid{ public: // initialize vector with 3 Points with val Points(0) Grid(vector&lt;Points&gt; v) : v_points(3, Points(0)) {}; private: vector&lt;Points&gt; v_points; }; </code></pre> <p>Note, we do it in initialization list. For POD class members, it makes no difference, it's just a matter of style. For class members which are classes, then it avoids an unnecessary call to a default constructor.</p> <blockquote> <p>Did I initialize constructor correctly and When writing a destructor for Grid do I need to write a destructor for vector like this: ~vecotr() or with delete or using iterator?</p> </blockquote> <p>You don't initialize constructor, rather in constructor initializer list you initialize class members. Since vector is not allocated with <code>new</code>, you will not <code>delete</code> it (call <code>delete</code>). It will be destroyed automatically when <code>Grid</code> is destroyed.</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. 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