title: Why does C++ invoke a copy constructor when iterating a container?
body: <pre><code>void ParticleGeneratorController::generate() {
      for( unsigned i = 0; i &lt; generators.size(); i++) {
          ParticleGenerator generator = *generators[i];
          generator.update();
      }
  }
  </code></pre>
  
  <p>Seems to call a copy constructor or something, but I didn't define one. I only have an explicit default constructor.</p>
  
  <p><a href="https://github.com/ChrisLundquist/Waveform/blob/master/src/models/particle_generator.h" rel="nofollow">https://github.com/ChrisLundquist/Waveform/blob/master/src/models/particle_generator.h</a></p>
  
  <p>Given my above code, by test fails. <a href="https://github.com/ChrisLundquist/Waveform/blob/master/spec/controllers/particle_generator_controller_spec.cpp#L21" rel="nofollow">https://github.com/ChrisLundquist/Waveform/blob/master/spec/controllers/particle_generator_controller_spec.cpp#L21</a></p>
  
  <p>When written as</p>
  
  <pre><code>void ParticleGeneratorController::generate() {
      for( unsigned i = 0; i &lt; generators.size(); i++) {
          generators[i]-&gt;update();
      }
  }
  </code></pre>
  
  <p>The test passes.</p>
  
  <p>Why is the copy constructor called in the first implementation? How is it generated by the compiler?</p>