我想做这样的事情:
Class myclass
{
private:
static int **board;
public:
myclass();
};
然后在cpp文件中
board = new int*[x];
for (int i = 0; i < x; i++) {
board[i] = new int[y];
}
我的目标是只有一个board
,无论我制作了多少对象。
最佳答案
你正在寻找Singleton
,链接在这里是关于这个的维基百科页面
https://en.wikipedia.org/wiki/Singleton_pattern
Singleton
的一个非常简单的实现是:
static int ** getBoard() {
if (!board) {
board = new int*[x];
for (int i = 0; i < x; i++) {
board[i] = new int[y];
}
}
else {
return board;
}
}
并且您可以使用myclass::getBoard()
来获取板。
根据您的要求,您可能需要一些变体。
关于c++ - C++类中的静态动态二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46311073/