title: C++11 range-based for loop on derived objects
body: <p>If I have a vector of pointers to a parent class, and the vector is initialized by instantiating objects derived from the parent class, then it appears that I can't use the range-based for loop so that I obtain the elements as derived objects. Here's a simple example:</p>
  
  <pre><code>#include &lt;vector&gt;
  
  class Parent {};
  class Derived : public Parent {};
  
  int main()
  {
      std::vector&lt;Parent*&gt; objects { new Derived(), new Derived() };
  
      for (Derived* d : objects) {    // ERROR
          // Use d
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Is there a clean way to do what I want (i.e., loop through the derived objects)? I know can do something like this:</p>
  
  <pre><code>for (Parent* p : objects) {
      Derived* d = static_cast&lt;Derived*&gt;(p);
      // Use d
  }
  </code></pre>
  
  <p>But is this the cleanest way with C++11? What are some other alternatives?</p>