title: Using variable from constructor in member function
body: <p>I have the following class which has a constructor that reads in a txt file which contains the size of <code>object</code> which the constructor should make. Then I need the function <code>read()</code> to pick up where the constructor stopped, but for some reason it starts at the top of the file again. How is this fixed?</p>
  
  <pre><code>class Reader {
  public:
      Reader(const char* file): _file(file) {
          _ptr = 0;
          ifstream _file(file);
  
          _file &gt;&gt; word;
          if(word!="BEGIN") {
              //Raise error.
          }
  
          _file &gt;&gt; word; //Reads in next word.
          if(word=="SIZE") {
              _file &gt;&gt; size_x;
              _file &gt;&gt; size_y;
  
              _ptr = new Object(size_x,size_y);
          }
          else {
             //Raise error.
          }
  
          _file &gt;&gt; word;
  
          while(word=="POSITION") {
              int readoutID;
              int ix;
              int iy;
              _file &gt;&gt; readoutID &gt;&gt; ix &gt;&gt; iy;
  
              //Set ID to position
  
              _file &gt;&gt; word;
          }
  
          std::cout &lt;&lt; "End of definition: " &lt;&lt; word &lt;&lt; std::endl;
      }
  
      bool read(){
          std::cout &lt;&lt; word &lt;&lt; std::endl; // This word should be the one where the constructor stopped.
      //Returns False at the end if file.
      }
  
  private: 
      Object* _ptr;
      std::ifstream _file;
      std::string word;
  </code></pre>
  
  <p>My main file looks like this:</p>
  
  <pre><code>int main(){
  
  Reader r("file.dat");
  
  while(r.read()) { 
  //Function that prints out the values of read() 
  } 
  
  }
  </code></pre>