I have a "Cell" class, with a "name" attribute. On other hand, I've got the main class with a bucle that generate a new array of chars, and then try to set the "name" of Cell with this value, but it doesn't run successfully.
Next code runs right:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int i = 0;
while ( i < 8 )
{
if ( i % 2 == 0 )
{
board[0][i].setName ( (char*) "Parell" );
}
else
{
board[0][i].setName ( (char*) "Imparell" );
}
printf ( "%dº. %s\n", i, board[0][0].getName() );
i++;
}
I think the problem is that you are not assigining the value of the string to name but its address. The value in that address keeps changing during the while loop iteration.
name = new_name; // name now points to whatever new_name was pointing.
Instead I would reccomend you not to use pointers in your class implementation. If you do use them, you will have to very carefully manage dynamic memory allocation / destruction.
And which is the solution? Because the function getName can't return an array of chars without a pointer I think. The name has to be a string, but I don't know how it's possible without pointers.
However, in your application it seems that the name will store the coordinates of the particular Cell. If such is the case, it can be implemented simply by member variables xCoord and yCoord instead of the compund name "name".