title: Generating data structures by parsing plain text files
body: <p>I wrote a file parser for a game I'm writing to make it easy for myself to change various aspects of the game (things like the character/stage/collision data). For example, I might have a character class like this:</p>
  
  <pre><code>class Character
  {
  public:
      int x, y; // Character's location
      Character* teammate;
  }
  </code></pre>
  
  <p>I set up my parser to read in from a file the data structure with syntax similar to C++</p>
  
  <pre><code>Character Sidekick
  {
      X = 12
      Y = 0
  }
  
  Character AwesomeDude
  {
      X = 10
      Y = 50
      Teammate = Sidekick
  }
  </code></pre>
  
  <p>This will create two data structures and put them in a map<code>&lt;std::string, Character*&gt;</code>, where the key string is whatever name I gave it (in this case Sidekick and AwesomeDude). When my parser sees a pointer to a class, like the teammate pointer, it's smart enough to look up in the map to fetch the pointer to that data structure. The problem is that I can't declare Sidekick's teammate to be AwesomeDude because it hasn't been placed into the Character map yet. </p>
  
  <p>I'm trying to find the best way to solve this so that I can have my data structures reference objects that haven't yet been added to the map. The two easiest solutions that I can think of are (a) add the ability to forward declare data structures or (b) have the parser read through the file twice, once to populate the map with pointers to empty data structures and a second time to go through and fill them in. </p>
  
  <p>The problem with (a) is that I also can decide which constructor to call on a class, and if I forward declare something I'd have to have the constructor be apart from the rest of the data, which could be confusing. The problem with (b) is that I might want to declare Sidekick and AwesomeDude in their own files. I'd have to make my parser be able to take a list of files to read rather than just one at a time (this isn't so bad I guess, although sometimes I might want to get a list of files to read from a file). (b) also has the drawback of not being able to use data structures declared later in the constructor itself, but I don't think that's a huge deal.</p>
  
  <p>Which way sounds like a better approach? Is there a third option I haven't thought of? It seems like there ought to be some clever solution to this with pointer references or binding or something... :-/ I suppose this is somewhat subjective based on what features I want to give myself, but any input is welcome. </p>