title: Calling a subclass method causes a segmentation fault
body: <p>I'm facing a strange issue. I wrote a Parent abstract class (implementing a pure virtual test() method) and its Child class (implementing the test() method).</p>
  
  <pre><code>class Parent
  {
      public :
          Parent();
          virtual ~Parent() = default;
  
          virtual bool test() const = 0;
  };
  
  class Child : public Parent
  {
      public :
          bool test() const;
  };
  </code></pre>
  
  <p>Then, I wrote a "Grid" class which is supposed to contain a two dimension array of pointers to Parent. The array is done using the vector library : "_cells" is a width*height vector of pointers to Parent. _cells is filled during Grid object construction using dynamic allocation and freed in the destructor. Operator() (int a, int b) is overloaded in order to be able to call the Parent object using this pattern : myGrid(x,y).</p>
  
  <pre><code>class Grid
  {
          int _w, _h;
          std::vector&lt;Parent*&gt; _cells;
  
      public :
          Grid(int w = 0, int h = 0);
          ~Grid();
          Parent* &amp;operator()(int x, int y);
  
      private :
          void generate();
  };
  </code></pre>
  
  <p>In my main function, g is a first 2x2 grid created on the stack. Then, it is supposed to destroy g and to construct a new 4x4 grid in g. But it completely fails :</p>
  
  <pre><code>Grid g(2, 2);
  std::cout &lt;&lt; g(1,1)-&gt;test() &lt;&lt; std::endl; // Works perfectly
  g = Grid(4, 4); // Probably wrong, but don't throw an exception
  std::cout &lt;&lt; g(1,1)-&gt;test() &lt;&lt; std::endl; // SIGSEGV
  </code></pre>
  
  <p>I think that the issue comes from dynamic allocation/desallocation of each cell, but I didn't find a way to solve it.</p>
  
  <p>Here is my full code, I didn't succeeded in simplifying it more. I did my best. Sorry.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  
  class Parent
  {
      public :
          Parent();
          virtual ~Parent() = default;
  
          virtual bool test() const = 0;
  };
  
  Parent::Parent()
  {}
  
  class Child : public Parent
  {
  
      public :
          bool test() const;
  };
  
  bool Child::test() const
  {
      return true;
  }
  
  class Grid
  {
          int _w, _h;
          std::vector&lt;Parent*&gt; _cells;
  
      public :
          Grid(int w = 0, int h = 0);
          ~Grid();
          Parent* &amp;operator()(int x, int y);
  
      private :
          void generate();
  };
  
  Grid::Grid(int w, int h) : _w(w), _h(h), _cells(w*h)
  {
      generate();
  }
  
  Grid::~Grid()
  {
      for (auto cell : _cells)
          delete cell;
  }
  
  Parent* &amp;Grid::operator()(int x, int y)
  {
      return _cells[x*_w+y];
  }
  
  void Grid::generate()
  {
      int cell_num;
      for (cell_num = 0; cell_num &lt; static_cast&lt;int&gt;(_cells.size()); cell_num++)
          _cells[cell_num] = new Child();
  }
  
  int main()
  {
      Grid g(2, 2);
      std::cout &lt;&lt; g(1,1)-&gt;test() &lt;&lt; std::endl;
      g = Grid(4, 4);
      std::cout &lt;&lt; g(1,1)-&gt;test() &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Thanks.</p>