0

I have problem to cast one class to another which inheritance from the first one. This is my function:

unique_ptr<game_state<int>> new_state = c4->make_move(column);

And now I want to overwrite it so I've tried like this:

c4 = dynamic_cast<unique_ptr<connect4<int>>*>(new_state);

but it don't work. That's how I declare my c4 object:

unique_ptr<connect4<int>> c4;

and my class:

template <typename Move>
class connect4 : public game_state<typename Move>

Any ideas?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
PoweR
  • 13
  • 2
  • 1
    Are you sure you need `unique_ptr` to work with, why not just `connect4 *c4 = dynamic_cast*>(new_state.get());`? There is `std::dynamic_pointer_cast` for `std::shared_ptr`, but AFAIK there is no equivalent for `std::unique_ptr`. – sklott Nov 25 '20 at 11:31
  • Does this answer your question? [“Downcasting” unique_ptr to unique_ptr](https://stackoverflow.com/questions/21174593/downcasting-unique-ptrbase-to-unique-ptrderived) – rustyx Nov 25 '20 at 11:38
  • I now realised that better for me is to have c4 declared like this: `connect4 c4;`, cause my function work with it. So is it possible to cast `unique_ptr>` to `connect4` ? – PoweR Nov 25 '20 at 11:44
  • This design doesn't look good at all. If you always need to downcast the result of a method, then the method is not placed in the right class and doesn't return the right type. – n. m. could be an AI Nov 25 '20 at 16:48

0 Answers0