0

I have a class Game which contains an other object GameGrid:

Game.h

class Game
{
        public:
            Game(uint32_t GridSize);
        private:
            GameGrid _CurrentGrid;
}

Game.cpp

Game::Game(uint32_t GridSize) : _CurrentGrid(GridSize)
{
}

GameGrid.h

class GameGrid
{       
        public:
            GameGrid(uint32_t GridSize);
            void setGrid(const Grid& Grid);
            const GameGrid::Grid getGrid(void) const;
        private:
            Grid _Grid;
}

GameGrid.cpp

GameGrid::GameGrid(uint32_t GridSize)
{
    uint32_t i = 0;
    uint32_t j = 0;

    _Grid.assign(GridSize, std::vector<unsigned int>(GridSize));

    for (i = 0; i < GridSize; i++)
    {
        for (j = 0; j < GridSize; j++)
        {
            _Grid.at(i).at(j) = 0;
        }
    }
}

void GameGrid::setGrid(const GameGrid::Grid& Grid)
{
    _Grid = Grid;
}

const GameGrid::Grid GameGrid::getGrid(void) const
{
    return _Grid;
}

Now I have my application, which uses the game class

Game* MyGame = new Game(4);

How can I create a copy function for this pointer to the Game-Object, so that I can clone the object.

I´ve tried it with the assignment operator

Game& operator=(Game const& Ref);

Game& Game::operator=(Game const& Ref)
{
    if (&Ref != this)
    {
        this->~Game2048();
        new (this) Game2048(Ref);
    }

    return *this;
}

But this solution doesn´t work and my original object got changed too, when I change the clone.

Does someone has a hint for me?

Thank you!

Kampi
  • 1,798
  • 18
  • 26
  • 1
    You're setting your clone as a reference to the original grid, that is why you saw it change the original grid. If you need to make a copy of the original but protect the original from changing, copy by value not reference. – Ryan Wilson Aug 17 '18 at 13:47
  • 3
    1) Why do you need to use pointers, at all, in the code shown? 2) Copy-constructors are used for copying objects.. – Algirdas Preidžius Aug 17 '18 at 13:47
  • 'Game2048' is not a valid in Game.h, probably typo, pls edit – Korni Aug 17 '18 at 13:47
  • i have the feeling that all your problems started with using `new`. Why do you use `new` ? Whats wrong with the compiler generated copy constructor? – 463035818_is_not_an_ai Aug 17 '18 at 13:47
  • `this->~Game2048();` this line is highly suspicious to me, one should practically never have to call a destructor directly, much less so on `this` – Borgleader Aug 17 '18 at 13:51
  • The explicit destructor/placement-new combination belongs to the "you might not need it for your entire career" category of language details. – molbdnilo Aug 17 '18 at 14:10
  • See [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Thomas Matthews Aug 17 '18 at 16:09
  • See [Rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three) – Thomas Matthews Aug 17 '18 at 16:11

2 Answers2

7

Don't create the copy constructor yourself and let the compiler do it for you, then just use objects:

Game MyGame{4};
Game gameClone = MyGame;

There's no reason to use a raw pointer here.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

I agree to what @SombreroChicken hast said.

I just want to point out the difference between a deep copy and shallow copy.

If your class Contains some pointer as private member data, you have to be aware, that only the pointer is copied and not the data to which is being pointed. Here is a nice in depth explanation of what I said.

schorsch312
  • 5,553
  • 5
  • 28
  • 57