I'm making a text based adventure game (original, I know.) and I hit a problem. I want to let the player pick up an item, and then automatically put it where it belongs based on the subclass of Item
that it belongs to. I have it set up so that when the player types "get " followed by the name of an object in the current room, the following function is called:
void Game::PickUp(Item * item)
{
m_map.at(m_player.GetLoc())->RemoveItem(item); //Remove item from the room
m_player.AddItem(item); //Add item to the player
}
Within the Player
class I've overloaded AddItem(…)
as follows:
void AddItem(Armor* armor)
{
delete m_armor;
m_armor = armor;
}
void AddItem(Weapon* weapon)
{
delete m_weapon;
m_weapon = weapon;
}
void AddItem(Shield* shield)
{
delete m_shield;
m_shield = shield;
}
void AddItem(Item* item)
{
m_pack.push_back(item);
}
When I run the code, it only calls void AddItem(Item* item)
, which makes sense because that's what's passed into the previous function, but since the object being passed is held in a vector<Item*>
I can't pass it as anything else. Is there a way to detect the subclass of the object being passed and call the correct overload?