title: Why are pointers to the base class preferred over references?
body: <p>Generally it appears preferable in C++ that code that functions that take a parameter to a polymorphic object takes a pointer to it:</p>
  
  <pre><code>class Base {};
  class DerivedA : public Base {};
  class DerivedB : public Base {};
  
  void OperateOnObject(Base* obj) {};
  
  vector&lt;Base*&gt; objects;
  objects.push_back(new DerivedA());
  objects.push_back(new DerivedB());
  
  for (size_t i=0; i&lt;objects.size(); i++) {
    OperateOnObject(objects[i]);
  }
  </code></pre>
  
  <p>Why is it that <code>OperateOnObject()</code> is usually written to take a pointer to <code>Base</code> rather than a reference? Are there potential problems with slicing? (eg the vtable gets lost)</p>
  
  <p>I've seen <a href="https://stackoverflow.com/questions/9425822/why-use-base-class-pointers-for-derived-classes">this response</a> to a similar-sounding question, but it doesn't seem to address the same issue.</p>