How can I add and initialize objects in a vector without having the destructor for the existing objects called?
<p>I am working a homework assignment and have come across a problem when I added a destructor to my class.   </p>

<p>From Assignment:</p>

<blockquote>
  <p>The class will have a destructor that displays a message indicating that an object has "gone out of scope".</p>
</blockquote>

<p>I've implemented this in my <code>rectangle</code> class by outputting to an <code>ofstream</code> object:</p>

<pre><code>rectangle::~rectangle()
{
    RectOutput &lt;&lt; "Object with length " &lt;&lt; length &lt;&lt; " and width " &lt;&lt; width &lt;&lt; " is now out of scope." &lt;&lt; std::endl;
}
</code></pre>

<p>The problem that I'm running into is that, I put these objects into a vector to make it easy to output to a table. Everything looked really good until I added the destructor. Due to the way vectors copy their data to a new internal array and delete the old array to increase the size, the destructor is called and the message is output several times in the creation of the objects.</p>

<p>Part of the constraints of this homework is that I had to initialize some of the objects with certain values. The choice to use a vector was my own. Here is the code of the initalization of the objects: </p>

<pre><code>std::vector &lt;rectangle&gt; Rectangles;

//Initialize all of our rectangle objects within our vector
Rectangles.push_back(rectangle());
Rectangles.push_back(rectangle(7.1, 3.2));
Rectangles.push_back(rectangle(6.3));
Rectangles.push_back(rectangle(21.0, 21.0));
Rectangles.push_back(rectangle(Rectangles[1]));
</code></pre>

<p>And here is the output being produced by this:</p>

<blockquote>
  <p>Object with length 1 and width 1 is now out of scope.<br>
  Object with length 1 and width 1 is now out of scope.<br>
  Object with length 7.1 and width 3.2 is now out of scope.<br>
  Object with length 1 and width 1 is now out of scope.<br>
  Object with length 7.1 and width 3.2 is now out of scope.<br>
  Object with length 6.3 and width 1 is now out of scope.<br>
  Value of 21 is invalid. Values must be greater than 0.0 and less than or equal to 20.0. Default of 1.0 used.<br>
  Value of 21 is invalid. Values must be greater than 0.0 and less than or equal to 20.0. Default of 1.0 used.<br>
  Object with length 1 and width 1 is now out of scope.<br>
  Object with length 7.1 and width 3.2 is now out of scope.<br>
  Object with length 6.3 and width 1 is now out of scope.<br>
  Object with length 1 and width 1 is now out of scope.<br>
  Object with length 1 and width 1 is now out of scope.<br>
  Object with length 7.1 and width 3.2 is now out of scope.<br>
  Object with length 6.3 and width 1 is now out of scope.<br>
  Object with length 1 and width 1 is now out of scope.<br>
  Object with length 7.1 and width 3.2 is now out of scope.  </p>
</blockquote>

<p>How can I initialize these objects in a vector without having the destructor being called repeatedly like this? I also tried to start off with <code>std::vector &lt;rectangle&gt; Rectangles(5);</code>, however this actually seems to call my default constructor 5 times, filling the vector with 5 default <code>rectangle</code> objects and then when I add to it the same effect is seen, except it is destructing the default objects. </p>