title: c++ Access elements of an object in a vector of objects
body: <p>I have a vector of <code>MyClass</code> created pretty much in this way:</p>
  
  <pre><code>class MyClass {
      friend Class MyOtherClass;
  
  public:
      MyClass();
      ~MyClass()
  
      // some functions...
  
  private:
      MyOtherClass other;
      std::string hgnc_symbol;
      uint_64 start_p;
      uint_64 stop_p;
      int objID;
      std::vector&lt;MyClass&gt; list;
  };
  </code></pre>
  
  <p>I'm really having hard time with the iterator, as I can't figure out how to properly access the filed of the objects stored in the vector.</p>
  
  <p>I populate the <code>list</code> reading elements from a file. And everything is OK, because <code>list.size()</code> returns the right number of elements. When I want to retrieve an object by its <code>objID</code>, I use an iterator to iterate over the list's objects. This is the code i've been trying</p>
  
  <pre><code>MyClass getByID(int id) {
      std::vector&lt;MyClass&gt;::iterator it;
      for(it=list.begin(); it!=list.end(); ++it){
          if((*it)-&gt;objID == id) {
              std::cout &lt;&lt; "FOUND! " &lt;&lt; (*it)-&gt;objID &lt;&lt; "\n";
              return (*it);
          }
      }
  }
  </code></pre>
  
  <p>but in this case, the field <code>objID</code> <em>cannot be resolved</em>. Trying with </p>
  
  <pre><code>...
  if((*it).objID == id) 
  std::cout &lt;&lt; "ID = " &lt;&lt; (*it).objID &lt;&lt; "";
  ...
  </code></pre>
  
  <p>the field is resolved but it prints thousands of <code>ID = 0</code> as if all the objects added to the list were empty.</p>
  
  <p>Shall I create a vector of pointers to object (like <code>std::vector&lt;MyClass*&gt; list;</code>)? would it be of any help? I actually tried but nothing changes: still I can't access the fields of each object, even though the iterators returns the pointer to it.</p>