我想做这样的事情:

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()来获得董事会。

您可能会根据自己的需求想要一些变体。

10-07 19:17
查看更多