Matthew wrote:
Hi, I have a base class called Item with a virtual function act. It's
subclass Sword defines the function again doing something different
from the first. It store the sword in a vector of items and call the
act function and, as to be expected, it calls the one from Item. How
can I get it to call the one from sword?
If you have vector<Item> then you are stuffed because what
is stored in the vector is Item's the Sword part is stripped
off, lookup class slicing (no pun intended) in your book.
What you want is either a vector of pointer to Items
vector<Item*>, or more preferably a vector<
smart_pointer<Item> >. Where 'smart_pointer' is your
favourite wrapper for pointers (boost::shared_ptr at a pinch
I suppose).