title: What is a possible workaround for object slicing in c++?
body: <p>This is the code:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  using namespace std;
  class A {
  public:
    virtual const string f() const { return "A"; }
  };
  class B : public A {
  public:
    const string f() const { return "B"; }
  };
  int main(int ac, char** av) {
    vector&lt;A&gt; v;
    v.push_back(B());
    cout &lt;&lt; v.at(0).f() &lt;&lt; endl;
    return 0;
  }
  </code></pre>
  
  <p>Expected result is <code>B</code>, but it's <code>A</code>. As I understand <strong>object slicing</strong> is taking place. How to avoid it? Shall I store pointers in <code>vector</code> instead of object instances? Is this the only choice?</p>