Your default constructor Slot()
doesn't allocate space for the Pawn *color
, so it'll just point to some random location in memory. When you write to it you'll write to this random location.
You can set it like this:
Slot::Slot() : color(new Pawn)
{}
Here we're using the member initializer list to set color
to allocated memory before the body of the constructor is executed. It's conceptually the same as saying:
Slot::Slot()
{
color = new Pawn;
}
For integral types and pointers there's not much difference. However, if the member variables were class instances then the member variable initializer list is the only way to initialize classes that do not have a default constructor.
However, since enum
is effectivly an int there's no point is using a pointer. Just declare it as:
Pawn color;
And get/set it like this:
void Slot::setColor(Pawn col)
{
color = col;
}
Pawn Slot::getColor()
{
return color;
}