You could also use #define
which is more C-style but also more performant than enums. The reason, why this is the fastest is that the compiler replaces every instance of eg ROW
with the actual calue (8).
Furthermore, the Java compiler also replaces static final
-variables to the actual byte code (which can sometimes be a problem, see here)
In your example that would be:
#define ROWS 8;
#define COLUMNS 8;
class Board {
public:
Board(int m[ROWS][COLUMNS]);
private:
Tile *m_TileMap[ROWS][COLUMNS];
};
But be aware of the visibilities and the adress space - you shouldn't put definitions like the in the header, because you would then force them upon everybody.
For the header, this would be preferred: Let's say you have a header file (board.h) in which you specify
class Board {
public:
static const int ROWS = 8;
static const int COLUMNS = 8;
//...
};
So everybody including your board.h can use Board::ROWS
and Board::COLUMNS
. (this is intended behavior)
See this answer for reference.
Another difference worth mentioning is that these makros are untyped in comparison to const
, which is typed (has type int for example while the defined value has none). Additionally, there are also dynamic constants, but that should be a bit too far off topic - further reading here.
But also the defines in C/C++ can do much more, see here for example.
Another thing a bit prospective: the constant strings C++ equivalent of Java static final String (first answer)