0
void Player::move(Board &board, Solver &solver){
   Position* best = solver.find_best_move(&board);
   cout<<"Score: "<<best->get_score()<<endl;
   cout<<"Board: ";
   best->get_board()->print_board();
   board = *(best->get_board());
   Board * b(best->get_board());
   cout<<"TEST: ";
   b->print_board();
   board = *b;
  }

I'm trying to make the actual board reference equal to the new board after calling the function. Board is an abstract class and get_board() is returning a pointer to a Board but it is actually a subclass of board which has an extra attribute. However,after the move function is called, the board is the same board as it was before the call to move. Is it possible to assign a subclass to a pointer to an abstract super class, while modifying the actual value? The issue of slicing seems to be occurring.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
carboncomputed
  • 1,630
  • 3
  • 20
  • 42

1 Answers1

0

I would use a Board* pointer instead of a Board& reference, especially since a subclass is involved:

void Player::move(Board **board, Solver &solver)
{ 
   Position *best = solver.find_best_move(*board); 
   cout << "Score: " << best->get_score() << endl; 
   *board = best->get_board(); 
   cout << "Board: "; 
   (*board)->print_board(); 
} 

Player p;
Solver solver;
Board *b = ...;
p.move(&b, solver);

Or:

void Player::move(Board* &board, Solver &solver)
{ 
   Position *best = solver.find_best_move(board); 
   cout << "Score: " << best->get_score() << endl; 
   board = best->get_board(); 
   cout << "Board: "; 
   board->print_board(); 
} 

Player p;
Solver solver;
Board *b = ...;
p.move(b, solver);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Agreed, you should mention aboul the potentional memory leak: one must make sure the original pointee is (or will be) destroyed before assigning a new value to the pointer. – J.N. Mar 07 '12 at 23:38
  • Would this work even if I'm passing in a pointer subclass of Board, say ChessBoard* board = new ChessBoard, and I pass it into move(&board,solver) – carboncomputed Mar 07 '12 at 23:55
  • @J.N: I was thinking more of the issue of slicing the reference to the subclass instance into a `Board` instance so all subclass information is lost. – Remy Lebeau Mar 07 '12 at 23:55
  • @user979663: it will work as long as a `ChessBoard` or subclass is always being returned. But if a different type of `Board` can be returned that is not related to `ChessBoard`, then you would have to declare the variable as a `Board*` instead of a `ChessBoard*`. Also, if you `new` the original `Board`, don't forget to `delete` it when you are done using it. If `move()` is going to change the pointer, then either `move()` has to `delete` the original, or else the calling code needs to remember the original so it can `delete` it after `move()` exits. – Remy Lebeau Mar 07 '12 at 23:58
  • Aha, I understand everything now. What I was trying to do was change what the point points to. The clue I needed was to use a double pointer. Thank you Remy, Ive been struggling with this problem all day and I felt I tried everything. Thanks – carboncomputed Mar 08 '12 at 00:06