I'm having a problem using inheritance and the STL list library...
Say, I have an abstract base class with two derived classes (where all comparison operators are defined). The list is declared as
list<StoreItem*> items;
I'm inserting a derived class (of the abstract base class, StoreItem) called either Food or Clothing. I make a new StoreItem pointer that's about to be inserted:
StoreItem* item = new Food(arguments here);
Now, I'm wanting to insert this new item (in order) to the list, and my attempt is this:
list<StoreItem*>::iterator iter;
for (iter = inventory.begin(); iter != inventory.end(); iter++)
{
if (*item < **iter)
break; // break out to insert
}
inventory.insert(iter, item);
Is there anything I'm doing wrong? Also, how would I pull the information from the inventory? (ex: Food tempFruit(**iter) using the copy constructor).
Thank you in advance! Have a good day.
You are assuming that the item you are pulling from the list is a
Food
instance; however, the compiler doesn't know that. When you construct a new instance ofFood
from an item in the list (an item with apparent typeStoreItem
), you are trying to callFood::Food(const StoreItem)
or something compatible. Why? Because the iterator points to aStoreItem*
that could be an instance of aStoreItem
object, or an instance of any class derived fromStoreItem
, such asFood
.As other posters have commented, polymorphism is a key to success. Do you really need to know that the item is a
Food
? If not, then access the interface shared by all store items (like price, serial-number, etc.). If you need to know something specific about the item, then you can try to infer its type: