title: having to use pointers to abstract class causes difficulties
body: <p>My first question here...</p>
  
  <p>I want to use an abstract class <code>A</code> from which i derive a class <code>B</code> (and classes <code>B2</code>, <code>B3</code>, ...). I understood that in order to handle them uniformly, I have to use pointers to the base class, so here variables of type <code>A*</code>.</p>
  
  <p>In the example below, I want to fill <code>aa</code> which is of type <code>vector&lt;A*&gt;</code> in function <code>f</code>. Since I can only use references and the variables lose their scope at the end of <code>f</code>, I cannot access the members of the entries of <code>aa</code> in <code>main</code> anymore.</p>
  
  <p>What could be a solution to this problem?</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  
  class A {
      public:
          virtual int getVar(int, int) = 0;
  };
  
  class B : public A {
      public:
          B(std::vector&lt;std::vector&lt;int&gt; &gt; var) : var_(var) {};
          int getVar(int i, int j) { return var_[i][j]; }
      private:
          std::vector&lt;std::vector&lt;int&gt; &gt; var_;
  };
  
  void f(std::vector&lt;A*&gt;&amp; aa) {
      std::vector&lt;std::vector&lt;int&gt; &gt; var(1, std::vector&lt;int&gt;(1));
      var[0][0] = 42;
  
      B b(var);
  
      aa.push_back(&amp;b);
  }
  
  int main() {
      std::vector&lt;A*&gt; aa;
  
      f(aa);
  
      std::cout &lt;&lt; aa[0]-&gt;getVar(0, 0) &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>