title: c++ loop through array of objects
body: <p>Im newbie in c++ and im having hard times with looping through array of objects. Objects are in this form : 
  Class * arr;
  (in my case : Persons * person_list; )</p>
  
  <p>PROBLEM: I have no idea how to loop through object array, i want to call functions on them, for example, as you can see my code has Persons class with function printPersonsInfo() and i have array decleration in main file ( array personList), i want to be able to do something like 
  for(Persons p : personList) p.printPersonInfo(); </p>
  
  <p>Tried to make many kind of loops, unsuccessful for loops with unsuccessful size of arrays, nothing works so far. Here comes the code :</p>
  
  <p>Main file :</p>
  
  <pre><code>int main()
  {
  
      //reading objects from file into array
      Persons * personList = Files::createPersonsArray();
  
      //reading objects from file into array
      Auto * autoList = Files::createAutoArray();
  
      return 0;
  }
  </code></pre>
  
  <p>One of Object classes ( both are pretty much the same) :</p>
  
  <pre><code>class Persons
  {
  public:
      int id;
      string name, surename, phone;
  
      Persons() {
          //required empty constructor
      }
  
      Persons(int i, string n, string s, string p) {
          this-&gt;id = i;
          this-&gt;name = n;
          this-&gt;surename = s;
          this-&gt;phone = p;
      }
  
  
      void printPersonInfo() {
          cout &lt;&lt; endl;
          cout &lt;&lt; "ID : " &lt;&lt; id &lt;&lt; endl;
          cout &lt;&lt; "Name : " &lt;&lt; name &lt;&lt; endl;
          cout &lt;&lt; "Surename : " &lt;&lt; surename &lt;&lt; endl;
          cout &lt;&lt; "Phone : " &lt;&lt; phone &lt;&lt; endl;
      }
  
  };
  </code></pre>
  
  <p>Files.h class, where i read data from files and generate them into arrays :</p>
  
  <pre><code>class Files
  {
  public:
  
      static Persons * createPersonsArray() {
  
          string textLine;
  
          // Read from the text file
          ifstream PersonsFile("Persons.txt");
  
          //counter
          int count = 0;
          //counting records in file
          while (getline(PersonsFile, textLine))
              count++;
  
          //return to start of file
          PersonsFile.clear();
          PersonsFile.seekg(0, ios::beg);
  
          //making array of persons
          Persons* person_list = new Persons[count];
  
          //total persons found
          cout &lt;&lt; "Lines of persons :" &lt;&lt; count &lt;&lt; endl;
  
          int i = 0;
          // Use a while loop together with the getline() function to read the file line by line
          while (getline(PersonsFile, textLine)) {
  
              //explodes line of person info into parts
              auto parts = explode(  textLine,'#');
  
              //stoi converts ID from string to int
              person_list[i] = Persons(stoi(parts[0]), parts[1], parts[2], parts[3]);
  
              //increased counter
              i++;
          }
  
          // Close the file
          PersonsFile.close();
  
          return person_list;
      }
  
      static Auto* createAutoArray() {
  
          string textLine;
  
          // Read from the text file
          ifstream AutoFile("Auto.txt");
  
          //counter
          int count = 0;
          //counting records in file
          while (getline(AutoFile, textLine))
              count++;
  
          //return to start of file
          AutoFile.clear();
          AutoFile.seekg(0, ios::beg);
  
          //making array of persons
          Auto* auto_list = new Auto[count];
  
          //total persons found
          cout &lt;&lt; "Lines of autos :" &lt;&lt; count &lt;&lt; endl;
  
          int i = 0;
          // Use a while loop together with the getline() function to read the file line by line
          while (getline(AutoFile, textLine)) {
  
              //explodes line of person info into parts
              auto parts = explode(textLine, '#');
  
              //stoi converts ID from string to int
              auto_list[i] = Auto(stoi(parts[0]), stoi(parts[1]), stoi(parts[2]), parts[3], parts[4], parts[5]);
  
              //increased counter
              i++;
          }
  
          // Close the file
          AutoFile.close();
  
          return auto_list;
      }
  
  
      //explode function
      static vector&lt;string&gt; explode(string const&amp; s, char delim)
      {
          vector&lt;string&gt; result;
          istringstream iss(s);
  
          for (string token; getline(iss, token, delim); )
          {
              result.push_back(move(token));
          }
  
          return result;
      }
  
  
      //find owner info based on id
      static void findCarOwnerInfo(int id) {
  
          string textLine;
  
          // Read from the text file
          ifstream PersonsFile("Persons.txt");
  
          //if file is not empty
          if (PersonsFile.peek() != ifstream::traits_type::eof()) {
  
              //counting records in file
              while (getline(PersonsFile, textLine)) {
                   auto parts = explode(textLine, '#');
  
                   //if person id matches
                   if (stoi(parts.at(0)) == id) {
                       cout &lt;&lt; endl;
                       cout &lt;&lt; "(ID:"&lt;&lt;parts.at(0)&lt;&lt;") Owner info ---------" &lt;&lt; endl;
                       cout &lt;&lt; "Name : " &lt;&lt; parts.at(1) &lt;&lt; endl;
                       cout &lt;&lt; "Surename : " &lt;&lt; parts.at(2) &lt;&lt; endl;
                       cout &lt;&lt; "Phone : " &lt;&lt; parts.at(3) &lt;&lt; endl;
                   }
              }
          }
          else {
              cout &lt;&lt; "error finding persons." &lt;&lt; endl;
          }
      }
  };
  </code></pre>
  
  <p>Since i shared Persons class and reading from files, here is how i store my info in Persons.txt:</p>
  
  <pre><code>ID#NAME#SURENAME#PHONE
  </code></pre>
  
  <p>The idea is to read files, create arrays of objects from them and pass them to the main class.</p>
  
  <p>Im using C++14 if that makes any difference.</p>
  
  <p>Thanks.</p>