473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Inheritance Issues

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
5 1578
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

"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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
6
by: Pascal Polleunus | last post by:
Hi, I'm wondering if there could be problems related to inheritance in the following scenario (with PostgreSQL 7.4.1)... 1 A-table, abstract. Max 10 B-tables that inherit from A, with...
5
by: jao | last post by:
My company's product uses Postgres 7.4.3. Postgres is working well for us, and we've worked through many performance issues by tweaking the schema, indexes, and posgresql.conf settings. ...
8
by: mmu2643 | last post by:
Hi, I had a question regarding multiple inheritance. class B1{ public: int a; };
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
4
by: Joe | last post by:
We are working on a project that requires 3rd parties (ie, consultants) to use our ASP.net webforms in a very reusable manner. The big catch is that we are not allowed to give them source. There...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
18
by: GD | last post by:
Please remove ability to multiple inheritance in Python 3000. Multiple inheritance is bad for design, rarely used and contains many problems for usual users. Every program can be designed only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.