The Student Room Group

c++ 2d array help

If I had a 2d array of pointers similar to type *board[8][8] declared privately in a class what would the best way to create a "getter" function to the be able to then pass the array into another function outside the class the array is in.

One very simple way to solve this would be to declare the board as public but that feels like cheating to me.

P.S. Sorry if that's worded very confusingly and any help would be appreciated :biggrin:
It depends if you want a getter function that returns a pointer to the original (so changes go back to the board) or if you want to return a copy.

The thing to be aware of with the first is that you need to make sure you don't free the object containing the board before you're done with it. With the second, you'd need to remember to free the copy when you're done with it (though that depends on whether you're using some kind of smart pointer).

Quick Reply

Latest

Trending

Trending