title: Saving a pointer vs passing value as const
body: <p>This is something from the theory of c++ which I never quite got. </p>
  
  <p>I have very large list of small structures (in size) called nodes inside Class A.
  So I have:</p>
  
  <pre><code>private:
     QList&lt;Node&gt; nodes;
  </code></pre>
  
  <p>Now Class A has instance of Class B (called cB) and for one of its functions it requires to iterate over nodes and just read them (it will not modify the information in any way). </p>
  
  <pre><code>function ClassB::readNodes(){
     for (int i = 0; i &lt; nodes.size(); i++){
         // READ nodes and do stuff with that information
     }
  }
  </code></pre>
  
  <p>So as far as I know there two ways to approach this. This is my current approach Inside Class B I have:</p>
  
  <pre><code>public:
      void setNodes(const QList *list) {nodes = list;}
  private:
      QList&lt;Node&gt; *nodes;
  </code></pre>
  
  <p>And then somewhere in Class A I do: </p>
  
  <pre><code>cb.setNodes(&amp;nodes);
  </code></pre>
  
  <p>The objective of this approach is that I want to avoid copying a very large data structure each time <code>readNodes()</code> is called, and also there is only one copy of the data structure in memory. This is important because Class A will continually change nodes.</p>
  
  <p>The second approach is much simpler and more clear in my opinion. Simply define <code>readNodes</code> as:</p>
  
  <pre><code>function ClassB::readNodes(const QList&lt;Node&gt; &amp;nodes){
     for (int i = 0; i &lt; nodes.size(); i++){
         // READ nodes and do stuff with that information
     }
  }
  </code></pre>
  
  <p>But I'm not totally sure that this will insume a performance penalty. As far as I understand it this approach also treats nodes as a reference and so no deep copy of nodes occurs. Is this correct?</p>