virtual functions and poniters to subclasses
Hi, i have a base class called Item which contains subclasses, Book, Serial and Newspaper. I have a virtual function called dispInfo which will display the information for that particular type of item. I have another function called GetItem(&Item), which searches through a list of Items for the one with a particular title. This returns a pointer to an Item. My question is how can i use this pointer as a Book, Serial or Newspaper instead of an item. I have something like this, but it always returns an Item.
[tag]
Code:
Item* ItemListNode::GetItem(Item &anItem)
{
if(FindItem(anItem)) //increases the iterator until found
{
return &*position; //position is a list iterator
}
return NULL;
}
[/tag]
Meanwhile...in main()
[tag]
Code:
Book*aBook=NULL;
anItem=ItemDatabase.GetItem(newBook);
anItem.disInfo();
[/tag]
The output is: "I AM AN ITEM!" instead of "I AM A BOOK!"
Can someone please help!?
Ok, could u explain i bit more please
Yes i have list<item> which stores all the items. Could you give me a short example in code which demonstrates what you said because i dont exactly understand what you mean.