fire in the hole
fire in the hole

Reputation: 1201

How to create an array of two dimensional arrays in C++

suppose I have a bunch of two dimensional arrays of type bool[4][4] like this:

class A
{
public:
    static const int SIZE = 4;
    static constexpr bool blocks_S[SIZE][SIZE]={    {0,1,1,0},
                                                    {1,1,0,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_SR[SIZE][SIZE] = { {1,1,0,0},
                                                    {0,1,1,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_L[SIZE][SIZE] = {  {0,0,1,0},
                                                    {1,1,1,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_LR[SIZE][SIZE] =  {{1,1,1,0},
                                                    {0,0,1,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_Box[SIZE][SIZE] = {{1,1,0,0},
                                                    {1,1,0,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};
    static constexpr bool blocks_Bar[SIZE][SIZE] = {{1,1,1,1},
                                                    {0,0,0,0},
                                                    {0,0,0,0},
                                                    {0,0,0,0}};  
};

and I want to make an array called block_types that holds a reference to all these two dimensional arrays so I can for example do: block_types[0] and recieve: blocks_S. how can I construct such an array (without using std::vector etc.) and what should be the type block_types? I tried auto and it picked up the wrong type.

auto block_types = { A::blocks_S, A::blocks_SR,
                     A::blocks_L, A::blocks_LR,
                     A::blocks_Box, A::blocks_Bar};

Upvotes: 1

Views: 193

Answers (1)

M.M
M.M

Reputation: 141658

The initializer for an array must be a braced list of element values -- this applies recursively. The initializer can't be another array. In other words, it's not possible to initialize multiple elements of an array by giving one initializer which is itself an array.

If you need two separate arrays with the same content you'll have to give them the same initializer: e.g. by writing it out twice, or using a #define macro, or generating the initializer by a constexpr function.


To fulfill your requirement:

I can for example do: block_types[0] and recieve: blocks_S

you don't need to create another array; you could use an array of pointers:

using row_t = const bool[A::size];
row_t *block_types[] = { A::blocks_S, A::blocks_SR,
                 A::blocks_L, A::blocks_LR,
                 A::blocks_Box, A::blocks_Bar};

after which block_types[0] can be used similarly to A::blocks_S.

Upvotes: 5

Related Questions