title: How to avoid pointers when using dynamic polymorphism?
body: <p>For a long time now I've perceived pointers, <code>new</code> and <code>delete</code> somewhat unnecessary in C++ unless dealing with long-lived objects, with references being a cleaner alternative that fits better in the RAII model. However, I am still unable to ascertain how to avoid pointers when using dynamic polymorphism in C++.</p>
  
  <p>Supose we have these clasess:</p>
  
  <pre><code>class A
  {
  public:
      virtual void a() const = 0;
  };
  
  class B : public A
  {
      virtual void a() const
      {
          std::cout &lt;&lt; "B";
      }
  };
  
  class C : public A
  {
      virtual void a() const
      {
          std::cout &lt;&lt; "C";
      }
  };
  
  void invoke(const A&amp; obj)
  {
      obj.a();
  }
  
  int main()
  {
      B b;
      invoke(b); // Prints B
  }
  </code></pre>
  
  <p>Object can be passed to <code>invoke</code> as a reference and there are no pointers involved (well, at least from the programmer's perspective). However, the above example is essentially static polymorphism.</p>
  
  <p>If I wanted to make the type of <code>b</code> dependent on something else, I would have to use pointers:</p>
  
  <pre><code>int main()
  {
      A* a;
      if (something)
          a = new B;
      else
          a = new C;
  
      invoke(*a); // Prints B
      delete a;
  }
  </code></pre>
  
  <p>This looks ugly as hell to me. Sure, I could use smart pointers:</p>
  
  <pre><code>int main()
  {
      std::unique_ptr&lt;A&gt; a;
      if (something)
          a.reset(new B);
      else
          a.reset(new C);
      invoke(*a); // Prints B
  }
  </code></pre>
  
  <p>But smart pointers are just wrappers for pointers.</p>
  
  <p>I would like to know if there is a way to avoid this and utilize polymorphic classes without the use of pointers.</p>