Windows XD Windows XD - 1 month ago 17
C++ Question

C++ Copy pointer class

I have a problem. I'm kinda new to C++, so please keep that in mind ^^ So I have function called

::Save( Player* pPlayer )
in which I'm inserting into map
m_mapSAVEDPLAYERS
copy of the player. But firstly I need to create copy of that player, but how? Should I make
Player* pNewPlayer = new Player();
and then...
pNewPlayer = pPlayer
or
*pNewPlayer = *pPlayer
? It has to be on new address so it won't get modified later. Thanks for help.

Answer

You need to call a copy c'tor.

Player* pNewPlayer = new Player(*pPlayer);

And be sure not to dereference a null valued pointer.

Comments