title: How to make the for each loop function in C++ work with a custom class
body: <p>I'm new to C/C++ programming, but I've been programming in C# for 1.5 years now. I like C# and I like the List class, so I thought about making a List class in C++ as an exercise.</p>
  
  <pre><code>List&lt;int&gt; ls;
  int whatever = 123;
  ls.Add(1);
  ls.Add(235445);
  ls.Add(whatever);
  </code></pre>
  
  <p>The implementation is similar to any Array List class out there. I have a <code>T* vector</code> member where I store the items, and when this storage is about to be completely filled, I resize it. </p>
  
  <p>Please notice that this is not to be used in production, this is only an exercise. I'm well aware of <code>vector&lt;T&gt;</code> and friends.</p>
  
  <p>Now I want to loop through the items of my list. I don't like to use <code>for(int i=0;i&lt;n; i==)</code>. I typed <code>for</code> in the visual studio, awaited for Intellisense, and it suggested me this:</p>
  
  <pre><code>for each (object var in collection_to_loop)
  {
  
  }        
  </code></pre>
  
  <p>This obviously won't work with my List implementation. I figured I could do some macro magic, but this feels like a huge hack. Actually, what bothers me the most is passing the type like that:</p>
  
  <pre><code>#define foreach(type, var, list)\
  int _i_ = 0;\
  ##type var;\
  for (_i_ = 0, var=list[_i_]; _i_&lt;list.Length();_i_++,var=list[_i_]) 
  
  foreach(int,i,ls){
      doWork(i);
  }
  </code></pre>
  
  <p>My question is: is there a way to make this custom List class work with a <code>foreach-like</code> loop?</p>