title: How do I create a list of pointers that point to objects within another list?
body: <p>I'm creating a simple graph where all nodes (or Vertices) are collected into a list.  Each node, in turn has a list of pointers that may point to multiple other nodes within the first list and call these edges.  For example, say I have a simple graph where <code>A -&gt; B</code> and <code>A -&gt; C</code> then Node <code>A</code> would have a list of pointers to <code>B</code> and <code>C</code> as it's edges.  </p>
  
  <p>As I create the graph, I first create the node, and populate it with anything pertinent to that node (weight or any other attributes).  I then add the node to the "Master" list of all nodes.  After that, I then pass the address of that node (now the last object in the list) into a function to add to any parent nodes:</p>
  
  <p><code>AddChildNode(&amp;MasterListOfNodes.back(), NameOfParent)</code></p>
  
  <p>The following is the code for adding the child node to the parent</p>
  
  <pre><code>void GraphReader::AddChildNode(Vertex * const aChildVertex, const string aParent)
  {
      for(list&lt;Vertex&gt;::iterator it = MasterListOfNodes.begin(); it != MasterListOfNodes.end(); it++)
      {
          if(it -&gt; getName().compare(aParent) == 0)
          {
              it -&gt; addEdge(aChildVertex);
              break;
          }
      }
  }
  </code></pre>
  
  <p>Where addEdge simply adds the new Child to the Parents "Edges" list:</p>
  
  <pre><code>const void Vertex::addEdge(Vertex * aEdge) {mEdges.push_back(aEdge);}
  </code></pre>
  
  <p>The problem that I'm having, is that the list of edges are pointing to a copy of the list object, and not the actual object within the list itself:</p>
  
  <p><img src="https://i.stack.imgur.com/Ivp4U.png" alt="VS and Printout of Root Nodes and Edges"></p>
  
  <p>Note: the addresses of the root nodes (command prompt output) were found with the following:</p>
  
  <pre><code>for (list&lt;Vertex&gt;::iterator it = MasterListOfNodes.begin(); it != MasterListOfNodes.end(); it++)
  {
      cout &lt;&lt; "Name: " &lt;&lt; it-&gt;getName() &lt;&lt; " | Address: {" &lt;&lt; &amp;(*it) &lt;&lt; "}" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>How do create my list of edges that point to the the master node?</p>