title: std::for_each, object with operator() overloaded not maintaining state
body: <p>In trying to answer <a href="https://stackoverflow.com/questions/7023539/create-vector-of-sizes-of-the-vectors-inside-a-2d-vector-vector-vector">this</a> question I came up with the following code:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt; 
  #include &lt;algorithm&gt;
  #include &lt;vector&gt;
  
  class Sizes
  {
  public:
      void operator() ( std::vector&lt;int&gt; v ) { 
          sizeVec.push_back( v.size() );  
      }
      std::vector&lt;int&gt; sizeVec;
  };
  
  void outFunc (int i) {
      std::cout &lt;&lt; " " &lt;&lt; i;
  }
  int _tmain(int argc, _TCHAR* argv[])
  {
      std::vector&lt;std::vector&lt;int&gt;&gt; twodVec;
  
      std::vector&lt;int&gt; vec;
      vec.push_back( 6 );
      twodVec.push_back( vec );
      vec.push_back( 3 );
      twodVec.push_back( vec );
      vec.push_back( 8 );
      twodVec.push_back( vec );
      vec.push_back( 3 );
      twodVec.push_back( vec );
  
      Sizes sizes;
      std::for_each( twodVec.begin(), twodVec.end(), sizes );
      std::for_each( sizes.sizeVec.begin(), sizes.sizeVec.end(), outFunc );
  
      return 0;
  }
  </code></pre>
  
  <p>Debugging this shows Sizes::operator() being called and the size of sizeVec increasing with each call as expected. However when the second std::foreach is called the sizeVec is empty... I've created a work around involving passing a vector into Sizes but does anyone know what's going on</p>