Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Getter返回C+中的二维数组+;_C++_Arrays_Function_2d_Getter - Fatal编程技术网

C++ Getter返回C+中的二维数组+;

C++ Getter返回C+中的二维数组+;,c++,arrays,function,2d,getter,C++,Arrays,Function,2d,Getter,这是我在SO上的第一篇帖子,尽管我已经在这里花了一些时间。 这里有一个函数返回2d数组的问题。我在我的游戏类中定义了一个私有的2d int数组属性int board[6][7],但是我不知道如何为这个属性创建一个公共getter 这些是我游戏的相关部分。h: #ifndef GAME_H #define GAME_H class Game { public: static int const m_rows = 6; static int const m_cols = 7;

这是我在SO上的第一篇帖子,尽管我已经在这里花了一些时间。 这里有一个函数返回2d数组的问题。我在我的游戏类中定义了一个私有的2d int数组属性int board[6][7],但是我不知道如何为这个属性创建一个公共getter

这些是我游戏的相关部分。h:

#ifndef GAME_H
#define GAME_H

class Game
{
public:
    static int const m_rows = 6;
    static int const m_cols = 7;

    Game();
    int **getBoard();

private:
    int m_board[m_rows][m_cols];

};

#endif // GAME_H
现在我想要的是game.cpp中的类似内容(因为我认为不带括号的数组名是指向第一个元素的指针,显然它不适用于二维数组):

因此,我可以在main.cpp中举例说明:

Game *game = new Game;
int board[Game::m_rows][Game::m_cols] = game->getBoard();
有人能帮我吗,我应该在游戏里放些什么


谢谢

不能按值将数组传入和传出函数。但有各种选择

(1) 使用
std::数组

int[][]
不涉及指针。它不是指向整数数组指针数组的指针,而是整数数组的数组

//row 1               //row2
[[int][int][int][int]][[int][int][int][int]]
这意味着一个
int*
指向所有这些。要获得行偏移量,请执行以下操作:

int& array_offset(int* array, int numcols, int rowoffset, int coloffset)
{return array[numcols*rowoffset+coloffset];}

int& offset2_3 = array_offset(obj.getBoard(), obj.m_cols, 2, 3);
    int *getBoard() {return m_board;}
    const int *getBoard() const {return m_board;}
private:
    int m_board[m_rows][m_cols];
//row 1               //row2
[[int][int][int][int]][[int][int][int][int]]
int& array_offset(int* array, int numcols, int rowoffset, int coloffset)
{return array[numcols*rowoffset+coloffset];}

int& offset2_3 = array_offset(obj.getBoard(), obj.m_cols, 2, 3);