Virtual functions mixed with vectors of objects

I'm trying to display the stats of each item in the player's inventory, but it's not going so well.

The vector's decleration:
std::vector<Object*> inventory;

Object is the base class for two derived classes (Weapon and Armor). In Object's class, there's this function:

virtual void DisplayStats();

In the Weapon's class there's this:
void DisplayStats() override;

And finally, in the player's DisplayInventory function, there's this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Player::InventoryDisplay()
{
	Weapon * WeaponBronzeSword;
	WeaponBronzeSword = new Weapon("Bronze Sword", 1, 3);
	inventory.push_back(*WeaponBronzeSword);

	for(int i = 0; i < inventory.size(); i++)
	{
		currentWeight += inventory[i].ReturnWeight(); //Calculate weight of inventory
	}
	
	cout << "Weight: (" << currentWeight << " lb / " << maxWeight << " lb)" << endl; //Display Current/max weight

	for(int i = 0; i < inventory.size(); i++)
	{
		inventory[i].DisplayStats();
	}

}


The last loop is giving me the problem. It calls the DisplayStats() function from the Object, rather than a Weapon. I don't know why, seeing it's a virtual function and the object I'm pushing back in the vector is a Weapon pointer.

I'm sorry if I worded it wrong, but I don't know why it's not working. From my understanding, overriding a virtual function is supposed to use the newer function rather than the virtual one, right?
To try to make this less confusing, if I put in Object's virtual DisplayStats function:

std::cout << "Hi!";

it would display "Hi!" in the last for loop. I want it to instead display what's in the weapon's DisplayStats() function, which is:

1
2
3
4
void Weapon::DisplayStats()
{
	cout << name << " | Damage: " << damage << " | Weight: " << weight << endl;
}


Thanks again.
I FOUND OUT.

I accidentally changed it so the vector didn't hold pointers. I changed it and now it's fixed.
Topic archived. No new replies allowed.