title: Call function in class stored in map
body: <p>I recently started using C++ a lot more and to get better at it I'm creating a basic voxel game that's a clone of what I did when learning Java. (think basic minecraft)</p>
  
  <p>The main game loop calls a <code>draw()</code> method in the <code>World</code> class, which then loops through all the chunks and calls a draw method on the chunks. In order to both save memory usage, and to have non-square shaped worlds, the chunks are stored in a map where the key is a class that stores the chunk position in <code>chunk space</code>. (chunk space is essentially <code>chunkx = floor(worldx/chunksize)</code>)</p>
  
  <p>I believe the way I am currently calling the <code>.draw()</code> function on the <code>chunks</code> in the <code>map</code> is not correct because the code to build the chunk mesh which is only suppose to be called once is being called over and over.</p>
  
  <p>The world's draw function:</p>
  
  <pre><code>void World::draw () {
      std::map&lt;ChunkPosition, Chunk&gt;::iterator it = chunks.begin();
      while (it != chunks.end()) {
          ChunkPosition pos = it-&gt;first;
          Chunk chunk = it-&gt;second; // I think I may need to use a pointer here somehow
  
          chunk-&gt;draw();
          it++;
      }
  }
  </code></pre>
  
  <p>The Chunk's draw function:</p>
  
  <pre><code>void Chunk::draw () {
      if(build){ // should only "build" the mesh once, is initialized to true
          build = false;
  
          ... mesh building code that is not relevant ...
  
          std::cout &lt;&lt; verticeSize &lt;&lt; std::endl; // this is called every loop cycle instead of once
  
          mesh.build (vertices, verticeSize, indices, indiceSize);
      }
  
      mesh.draw(); // draw the mesh every cycle
  }
  </code></pre>
  
  <p>And here is the Chunk class's structure</p>
  
  <pre><code>class Chunk {
      private:
          static const int size = 25;
          std::map&lt;Location, Block&gt; blocks;
          Mesh mesh;
          bool build = true;
      public:
          void setBlock (Location, Block&amp;);
          void setBlock (int, int, int, Block&amp;);
          Block *getBlock (Location);
          Block *getBlock (int, int, int);
          bool hasBlock (Location);
          bool hasBlock (int, int, int);
          void draw ();
  };
  </code></pre>
  
  <p>I have a single chunk in the <code>chunks</code> map at position <code>(0,0,0)</code>. Everything else about it is irrelevant to the question. </p>