C ++类中的静态动态二维数组

时间:2017-09-19 23:21:09

标签: c++ arrays class

我想做这样的事情:

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,无论我制作多少个对象。

2 个答案:

答案 0 :(得分:1)

您正在寻找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()来获得董事会。

根据您的要求,您可能需要一些变体。

答案 1 :(得分:0)

  

我的目标是只有一块板,无论我制造多少物体。

您非常接近您的需求。关键的想法仍然缺失:如何确定这是否是第一个ctor,以便您只启动静态板一次。事实证明,矢量也可以解决这个问题。

#include <chrono>
// 'compressed' chrono access --------------vvvvvvv
typedef std::chrono::high_resolution_clock  HRClk_t; // std-chrono-hi-res-clk
typedef HRClk_t::time_point                 Time_t;  // std-chrono-hi-res-clk-time-point
typedef std::chrono::milliseconds           MS_t;    // std-chrono-milliseconds
typedef std::chrono::microseconds           US_t;    // std-chrono-microseconds
typedef std::chrono::nanoseconds            NS_t;    // std-chrono-nanoseconds
using   namespace std::chrono_literals;          // support suffixes like 100ms, 2s, 30us

#include <iostream>
#include <vector>
#include <cassert>


class myclass
{
private:
   static std::vector<int> board;
   static int              maxCol;

   // add non-static parameters (unique to each instance)
   // here

public:

   // ctor
   myclass(int maxRows=0, int maxCols=0)
      {
         // key idea: how determine 1st ctor, so only init board 1 time
         if(0 == board.size())
         {
            std::cout
               << "  because board.size() is 0, \n"
               "  we know this is the first opportunity "
               "to reserve and initialize \n  "
               << maxRows << "*" << maxCols << " = " << (maxRows * maxCols)
               << " elements." << std::endl;

            // reserve space
            board.reserve(maxRows*maxCols); // now have all the space needed

            // with space available, initialize the elements
            for (int r=0; r<maxRows; ++r) {
               for (int c=0; c<maxCols; ++c) {
                  board.push_back(0); // init to valid number
               }
            }

            maxCol = maxCols;
         } // additional invocation do not affect board, parameters ignored
         else
            std::cout << "\n\n  parameters ignored because size is ("
                      << board.size() << ")" << std::endl;

         // add myclass ctor unique actions here
      }

   ~myclass() = default;


   // 2d access game to board
   // to 1d           from-2d
   size_t    gbIndx(int r, int c) { return ((r * maxCol) + c); }

   // void   gbIndx(size_t indx1d, int& r, int& c) {
      // simple arithmetic to determine both r and c from indx1d
   //}

   int exec()
      {
         std::cout << "\n  exec(): sizeof(board) = " << sizeof(board)
                   << "  board.size() = " << board.size() << std::endl;

         // do what now?

         return(0);
      }

   // add more methods here

}; // class myclass

std::vector<int> myclass::board; // static vector
int              myclass::maxCol = 0;


int main(int, char**)
{
   myclass mc(10,10);

   Time_t start_us = HRClk_t::now();

   int retVal = mc.exec();

   // subsequent invocation work, but do not affect board.size()
   myclass mc2;
   mc2.exec();

   myclass mc3;
   mc3.exec();


   auto  duration_us = std::chrono::duration_cast<US_t>(HRClk_t::now() - start_us);

   std::cout << "\n\n  t534.exec() duration  " << duration_us.count() << " us" << std::endl;
   return(retVal);
}

输出如下:

因为board.size()为0,   我们知道这是第一次保留和初始化的机会   10 * 10 = 100个元素。

exec():sizeof(board)= 24 board.size()= 100

参数被忽略,因为大小为(100)

exec():sizeof(board)= 24 board.size()= 100

参数被忽略,因为大小为(100)

exec():sizeof(board)= 24 board.size()= 100