I’m trying to construct a 2D array in the form of pointers-to-pointers. This doesn’t work:
bool** data = {
new bool[4] {true, true, true, true},
new bool[4] {true, false, false, true},
new bool[4] {true, false, false, true},
new bool[4] {true, true, true, true}
};
Is it possible? How should I be doing it?
EDIT:
Looks like I might be trying to do the wrong thing. I have a function that takes a 2D array of bools of an unknown size, along with integer width and height, as arguments. At present, the signature is:
foo(bool** data, int width, int height)
I want to be able to construct a literal for data, but I also need this function to work for any size of array.
You could have an array of arrays (sometimes referred to as a multi-dimensional array):
However, that isn’t convertible to
bool**, so if you need that conversion then this won’t work.Alternatively, an array of pointers to static arrays (which is convertible to
bool**):or if you really want dynamic arrays (which is almost certainly a bad idea):
or, perhaps, you could stick with arrays, and modify your functions to take references to arrays rather than pointers, inferring the dimensions as template parameters if you need to support different dimensions. This is only possible if the dimensions are always known at compile time.