I’m learning c++ and OOP and I have class called “Pion” and I have a class “Chessboard” and in the class “Chessboard” a need to declare a 2d array of pointers to the class “Pion” and initialize it to NULL pointers.
I know how to declare the 2d array but I don’t how to initialize it to NULL pointers.
declaring the 2d array should be something like this:
Pion *P[8][8];
but I don’t get the set to null pointers part.
Any help/tips are welcome.
[EDIT]
thanks for all the answers/tips but I’m still confused.
so i have a class called Chessboard and I need to create a 2d array of pointers that link to the “Pion” class, this array needs to be a class member and initialize it to NULL pointers.
so I have tried tried out things and I’ve come up with this
in the .h (header file) I’ve declared a private member class
Pion *P[8][8];
and within the concstructor I’ve put this
for (int x = 0; x < 8; ++x) {
for (int y = 0; y < 8; ++y) {
m_velden[x][y] = NULL;
}
}
This seems to be working but i’m not sure if this is the best solution
It depends on whether P is a local variable or a class member.
In the 1st case, you can initialize all elements to NULL at the moment of declaration
In the 2nd case, use
P()in the initialization list inChessboardconstructor.