Is it possible in c++ for a class to have a member which is a multidimensional array whose dimensions and extents are not known until runtime?

前端 未结 4 1065
攒了一身酷
攒了一身酷 2021-01-14 01:11

I originally asked using nested std::array to create an multidimensional array without knowing dimensions or extents until runtime but this had The XY Problem of trying to a

4条回答
  •  孤街浪徒
    2021-01-14 01:48

    Yes. with a single pointer member.

    A n multidimensional array is actually a pointer. so you can alocate a dynamic n array and with casting, and put this array in the member pointer.

    In your class should be something like this

    int * holder;
    
    void setHolder(int* anyArray){
      holder = anyArray;
    }
    

    use:

    int *** multy = new int[2][1][56];
    yourClass.setHolder((int*)multy);
    

提交回复
热议问题