c++ - How to assign a subclass to an abstract class pointer in a function? -
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 make actual board reference equal new board after calling function. board abstract class , get_board() returning pointer board subclass of board has attribute. however,after move function called, board same board before call move. possible assign subclass pointer abstract super class, while modifying actual value? issue of slicing seems occurring.
i use board*
pointer instead of board&
reference, since subclass 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);
Comments
Post a Comment