C++ - typeid(), used on derived class doesn't return correct type

前端 未结 3 738
情话喂你
情话喂你 2021-01-05 02:22

Maybe I\'m misunderstanding how inheritance works here, but here\'s my problem:

I have a class Option, and a class RoomOption that derives from it. I have another cl

3条回答
  •  北海茫月
    2021-01-05 03:22

    First of all yor getting the typeid of the shared_ptr.

    Then you should use dynamic_cast instead of typeid. E.g:

    if (dynamic_cast(player->getRoom()->getOption(0).get()) != 0){
        cout << "Its a RoomOption!" << endl;
    }
    

提交回复
热议问题