I am writing a chess game to familiarize myself with the C++ language. I have a Board class which contains a 2D array of pointers to Piece objects. I want to implement Board::operator such that the following is possible:
Board b; Piece *p = b;
I could just return a row of the array which is stored internally. However, I want to implement this in such a way that the pointer to the Piece cannot be overwritten. For example:
b.moveTo(0,0); // legal b = new Piece; // not legal *p = new Piece; // not legal (p is defined in the previous code block)
I believe the best way to do this is to use a wrapper class for the object returned by b which also implements operator. The first problem I have with this approach is that since a pointer to an object of the Board class is returned, I cannot declare any of the methods/arguments/return values as const, which seems like bad style. The second problem I have is that I do not know how to prevent the current Piece from being replaced via
*p = new Piece . Should I return a reference instead?