title: C++ copy object
body: <p>I have a class <code>Game</code> which contains an other object <code>GameGrid</code>:</p>
  
  <p>Game.h</p>
  
  <pre><code>class Game
  {
          public:
              Game(uint32_t GridSize);
          private:
              GameGrid _CurrentGrid;
  }
  </code></pre>
  
  <p>Game.cpp</p>
  
  <pre><code>Game::Game(uint32_t GridSize) : _CurrentGrid(GridSize)
  {
  }
  </code></pre>
  
  <p>GameGrid.h</p>
  
  <pre><code>class GameGrid
  {       
          public:
              GameGrid(uint32_t GridSize);
              void setGrid(const Grid&amp; Grid);
              const GameGrid::Grid getGrid(void) const;
          private:
              Grid _Grid;
  }
  </code></pre>
  
  <p>GameGrid.cpp</p>
  
  <pre><code>GameGrid::GameGrid(uint32_t GridSize)
  {
      uint32_t i = 0;
      uint32_t j = 0;
  
      _Grid.assign(GridSize, std::vector&lt;unsigned int&gt;(GridSize));
  
      for (i = 0; i &lt; GridSize; i++)
      {
          for (j = 0; j &lt; GridSize; j++)
          {
              _Grid.at(i).at(j) = 0;
          }
      }
  }
  
  void GameGrid::setGrid(const GameGrid::Grid&amp; Grid)
  {
      _Grid = Grid;
  }
  
  const GameGrid::Grid GameGrid::getGrid(void) const
  {
      return _Grid;
  }
  </code></pre>
  
  <p>Now I have my application, which uses the game class</p>
  
  <pre><code>Game* MyGame = new Game(4);
  </code></pre>
  
  <p>How can I create a copy function for this pointer to the Game-Object, so that I can clone the object.</p>
  
  <p>I´ve tried it with the assignment operator</p>
  
  <pre><code>Game&amp; operator=(Game const&amp; Ref);
  
  Game&amp; Game::operator=(Game const&amp; Ref)
  {
      if (&amp;Ref != this)
      {
          this-&gt;~Game2048();
          new (this) Game2048(Ref);
      }
  
      return *this;
  }
  </code></pre>
  
  <p>But this solution doesn´t work and my original object got changed too, when I change the clone.</p>
  
  <p>Does someone has a hint for me?</p>
  
  <p>Thank you!</p>