title: C++ store reference to class member in a vector
body: <p>Consider a Drag object: </p>
  
  <pre><code>class Drag {
  public:
      Drag(ofVec3f _pos);
      ofVec3f pos;
  }
  </code></pre>
  
  <p>Position is stored when a new instance is created:</p>
  
  <pre><code>Drag::Drag(ofVec3f _pos) {
      pos = _pos;
  }
  </code></pre>
  
  <p>Position is updated when mouse moves:</p>
  
  <pre><code>void Drag::mouseMoved() {
      pos.x = ofGetMouseX();
      pos.y = ofGetMouseY();
  }
  </code></pre>
  
  <hr>
  
  <p>In the application's main class (testApp in openframeworks):</p>
  
  <pre><code>class testApp : public ofBaseApp {
      public:
          vector&lt;Drag&gt; drags;
          vector&lt;ofVec3f *&gt; points;
  }
  </code></pre>
  
  <p>Create a Drag when the mouse is pressed and store its position in a vector called points:</p>
  
  <pre><code>void testApp::mousePressed(int x, int y, int button) {
      Drag drag = Drag(ofVec3f(ofGetMouseX(), ofGetMouseY(), 0));
      drags.push_back(drag);
      points.push_back(&amp;drag.pos);
  }
  </code></pre>
  
  <p>Now when the drag is moved, I can see its position update, but points[0] doesn't change:</p>
  
  <pre><code>void testApp::update(){
      if (!drags.size()) return;
      cout &lt;&lt; drags[0].pos.x &lt;&lt; ", " &lt;&lt; drags[0].pos.y &lt;&lt; endl;
      cout &lt;&lt; &amp;points[0]-&gt;x &lt;&lt; ", " &lt;&lt; &amp;points[0]-&gt;y &lt;&lt; endl;
  }
  </code></pre>
  
  <p>If the type of points is <code>vector&lt;ofVec3f&gt;</code> it seems that points[0] is a copy of the initial drags[0].pos. If it is <code>vector&lt;ofVec3f *&gt;</code> then it seems to store an address on memory that is equal to &amp;drag.</p>
  
  <p>How can I make points[0] point to drags[0].pos and get its x,y values updated when drags[0].pos.x and drags[0].pos.y are updated?</p>
  
  <p>How can I make points[0] be a <strong>reference</strong> to drags[0].pos?</p>
  
  <hr>
  
  <p><strong>Edit:</strong> Thanks to yonilevy for pointing me in the right direction. Here's the working  example, updated using std::list:</p>
  
  <pre><code>// testApp.h
  list&lt;Drag&gt; drags;
  vector&lt;ofVec3f *&gt; points;
  
  // testApp::mousePressed
  drags.push_back(Drag(ofVec3f(ofGetMouseX(), ofGetMouseY(), 0)));
  points.push_back(&amp;drags.back().pos);
  </code></pre>