By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Inheritance Issues

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 20 Nov 2003 12:50:00 -0800, ma******@nessiness.com (Matthew) wrote:

Did you declare act as virtual in the Sword class?
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?


Jul 22 '05 #2

P: n/a

"Russ Ford" <ru******@shaw.ca> wrote in message
news:ig********************************@4ax.com...
On 20 Nov 2003 12:50:00 -0800, ma******@nessiness.com (Matthew) wrote:

Did you declare act as virtual in the Sword class?


Not necessary (as long as the signature is the same).

The real issue is: *How* was the function called?
Polymorphism only works via a pointer or reference.

Matthew:

We can only make guesses like this since you show no code.

Show the code.

-Mike
Jul 22 '05 #3

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:qp*****************@newsread1.news.pas.earthl ink.net...

"Russ Ford" <ru******@shaw.ca> wrote in message
news:ig********************************@4ax.com...
On 20 Nov 2003 12:50:00 -0800, ma******@nessiness.com (Matthew) wrote:

Did you declare act as virtual in the Sword class?


Not necessary (as long as the signature is the same).


Clarification: if the signature is not the same, then
it's not an override anyway.

-Mike
Jul 22 '05 #4

P: n/a
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).

Jul 22 '05 #5

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:vq*****************@newsread1.news.pas.earthl ink.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:qp*****************@newsread1.news.pas.earthl ink.net...

"Russ Ford" <ru******@shaw.ca> wrote in message
news:ig********************************@4ax.com...
On 20 Nov 2003 12:50:00 -0800, ma******@nessiness.com (Matthew) wrote:

Did you declare act as virtual in the Sword class?


Not necessary (as long as the signature is the same).


Clarification: if the signature is not the same, then
it's not an override anyway.


If it's not virtual, it's never an override regardless of the signature.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.