lundi 8 août 2022

How to Store and retrieve the the correct Type of DerivedClass Pointer To/From Map of BaseClass Pointer

Hello i have question about Maps and Inheritance.
I have Map of BaseClass Pointer:

std::map<int,std::shared_ptr<Game>> GamesByID;

and inserted Derived Class Pointer into it like:

GamesByID.insert(make_pair(ID,std::make_shared<ActionGame>(ID,Title,Metacritic,Recommendations,Price,lvl)));

now i wanted to count how many ActionGame Pointer i had in my map with somthing like that


  for (auto& e : GamesByID)
        {
    if(typeid(e) == typeid(SurvivalGame)){
            ++count;
            
    }else{
        cout << typeid(e).name() << "  typeid" <<endl;
    }

}

But in the map ther are only BaseClass Pointer, now what is my mistake do i instert them wrong ?

Aucun commentaire:

Enregistrer un commentaire