arrays - C++ Copying Pointers -
hi know how copy contents of 2d array pointer in c++ location , set pointer when make changes on copied pointer nothing happens original data?
basically array pointer pieces on chessboard. goes piece * oldpointer = board[8][8]
. want copy contents in pointer including methods getvalue(), getcolor()
etc in pieces header file location , set pointer can operations there , test without having affect orginal data? read somewhere had use allocate()
im not sure. please help
in c++ define 2d array type follows (you need modern c++ compiler):
#include <array> typedef std::array<std::array<piece, 8>, 8> board_t;
if compiler doesn't support std::array
can use boost::array
instead:
#include <boost/array.hpp> typedef boost::array<boost::array<piece, 8>, 8> board_t;
now can use type above. can see need copy object pointer points:
board_t* oldpointer = new board_t; // oldpointer // make copy of instance of object oldpointer points // using copy-constructor board_t* newpointer = new board_t( *oldpointer ); // newpointer points newly created independent copy // more // clean delete oldpointer; // more newpointer // clean delete newpointer;
Comments
Post a Comment