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

前端 未结 3 739
情话喂你
情话喂你 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:02

    The type of object a shared_ptr<Option> points to is part of its value, not its type. So this line of code is broken:

    cout<<typeid(player->getRoom()->getOption(0)).name()<<endl; 
    

    You want this:

    cout<<typeid(player->getRoom()->getOption(0).get()).name()<<endl; 
    

    Or perhaps:

    cout<<typeid(*(player->getRoom()->getOption(0))).name()<<endl; 
    

    What typeid does is tell you the actual type of the thing you passed to it. You passed it a shared_ptr<Option>. It doesn't look inside the object to see what it contains.

    0 讨论(0)
  • 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<RoomOption*>(player->getRoom()->getOption(0).get()) != 0){
        cout << "Its a RoomOption!" << endl;
    }
    
    0 讨论(0)
  • 2021-01-05 03:25

    The typeid works differently for polymorphic (for classes having at least one virtual function) and non-polymorphic types :

    • If the type is polymorphic, the corresponding typeinfo structure which represents it is determined at run-time (the vtable pointer is commonly used for that purpose, but this is an implementation detail)

    • If the type isn't polymorphic, the corresponding typeinfo structure is determined at compile time

    In your case, you actually have a polymorphic class Option, but shared_ptr<Option> itsef isn't polymorphic at all. It basically is a container holding an Option*. There is absolutely no inheritance relation between Option and shared_ptr<Option>.

    If you want to get the real type, you first need to extract the real pointer from its container using Option* shared_ptr<Option>::get() :

    Option * myPtr = player->getRoom()->getOption(0).get();
    cout << typeid(*myPtr).name(); << endl;
    

    Or alternatively (it is exactly the same thing) :

    Option& myPtr = *player->getRoom()->getOption(0);
    cout << typeid(myPtr).name(); << endl;
    
    0 讨论(0)
提交回复
热议问题