Returning multidimensional array from function

后端 未结 7 958
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 03:51

How do I return a multidimensional array stored in a private field of my class?

class Myclass {
private:
   int myarray[5][5];
public:
   int **         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 04:25

    How do I return a multidimensional array hidden in a private field?

    If it's supposed to be hidden, why are you returning it in the first place?

    Anyway, you cannot return arrays from functions, but you can return a pointer to the first element. What is the first element of a 5x5 array of ints? An array of 5 ints, of course:

    int (*get_grid())[5]
    {
        return grid;
    }
    

    Alternatively, you could return the entire array by reference:

    int (&get_grid())[5][5]
    {
        return grid;
    }
    

    ...welcome to C declarator syntax hell ;-)

    May I suggest std::vector > or boost::multi_array instead?

提交回复
热议问题