您可以有一个数组数组(有时称为多维数组):
bool data[][4] = {
{true, true, true, true},
{true, false, false, true},
{true, false, false, true},
{true, true, true, true}
};
但是,这不能转换为 bool**,所以如果您需要这种转换,那么这将不起作用。
或者,一个指向静态数组的指针数组(是可转换为bool**):
bool data0 = {true, true, true, true};
bool data1 = {true, false, false, true};
bool data2 = {true, false, false, true};
bool data3 = {true, true, true, true};
bool * data[] = {data0, data1, data2, data3};
或者如果你真的想要动态数组(这几乎肯定是个坏主意):
bool * make_array(bool a, bool b, bool c, bool d) {
bool * array = new bool[4];
array[0] = a;
array[1] = b;
array[2] = c;
array[3] = d;
return array;
}
bool * data[] = {
make_array(true, true, true, true),
make_array(true, false, false, true),
make_array(true, false, false, true),
make_array(true, true, true, true)
};
或者,也许,您可以坚持使用数组,并修改您的函数以引用数组而不是指针,如果您需要支持不同的维度,则将维度推断为模板参数。这只有在编译时总是知道尺寸时才有可能。
template <size_t N, size_t M>
void do_something(bool (&array)[N][M]);
do_something(data);