0

I have a class for my console game engine, it has an array of characters for the map. In the constructor, I want to take parameters for the height and width of the map (the map is an array). I tried to declare and instantiate an array in the constructor however this array gets deleted after the constructor is done constructing. heres what it looked like:

class ConsoleGameEngine {
public:
    ConsoleGameEngine(mapheight, mapwidth) {
        char map[mapheight][mapwidth];
    }
}

this didnt allow any other methods to access the map array as it was destroyed when the constructor finished. I want to do something like this:

class ConsoleGameEngine {
public:
    char map;
    ConsoleGameEngine(mapheight, mapwidth) {
        map[mapheight][mapwidth];
    }
}

i dont want to use vectors, i want to use normal arrays. Thank you for reading.

  • 1
    What you want would result in a class of variable size. That wrecks `sizeof` and a number of other constructs like arrays, so it is illegal. I suggest using [a simple matrix class](https://stackoverflow.com/a/2076668/4581301) instead. – user4581301 Dec 01 '19 at 19:23

3 Answers3

0

That's not possible. Arrays must have compile-time constant sizes in C++. See this question for an explanation of why. You could dynamically allocate it, but you would have to manage the memory which can get complicated. std::vector is probably the best option, and you can use a 1D vector by mapping 2D indices into 1D indices which is more efficient than a vector of vectors.

eesiraed
  • 4,626
  • 4
  • 16
  • 34
0

Just a complement on @AlexanderZhang's answer.

The least bad way is to use an underlying 1D vector which will handle dynamic memory management for you. But unfortunately, C++ has no support for anything close to multi-D containers.

Here is an old question of mine explaining the problem, with a very detailed answer from NicolBolas

Anyway, you will find there an implementation attempt that I submitted on Code Review - this one was the third try, and you will see that trying to mimic standard containers over multi-D arrays soon leads to some bit of complexity...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

You can use a template. The parameters are known at compile time, so you can use them to define the dimensions of your array:

#include <iostream>
#include <cstdlib>

template < int nRows, int nCols>
class MyFunArray
{
  public:
  int m_nRows = nRows;
  int m_nCols = nCols;
  char map[nRows][nCols];

  bool SetValue(char val, int row, int col)
  { if (row >= 0 && row < m_nRows && col >= 0  && col < m_nCols)
    {   map[row][col] = val;
        return true;
    }
    return false;
  }

  char GetValue(int row, int col)
  { if (row >= 0 && row < m_nRows && col >= 0  && col < m_nCols)
      return map[row][col];
    else
      return '\0';
  }
};


int main()
{
    MyFunArray<3,4> myArr;
    myArr.SetValue('b', 1,2);
    std::cout << myArr.GetValue(1,2) << std::endl;
}

I have added Set/Get functions, just to code the bound limits. If you feel brave (and irresponsible) you can access map directly.

Ripi2
  • 7,031
  • 1
  • 17
  • 33