473,385 Members | 1,647 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,385 software developers and data experts.

Inheritance Puzzler

I'm having an inheritance problem. My mom says she just won't leave me
her Three Stooges DVD collection when she's gone. Okay, just a bit of
OO levity. Here's the real problem:

We have AbsBase1, AbsDerived1, AbsBase2, ConcreteBase, and
ConcreteDerived.

The following inheritance hierarchy applies (arrows point to base
classes):

AbsBase1<---AbsBase2<---ConcreteDerived

ConcreteBase<---ConcreteDerived

Note that ConcreteDerived is the same class type on both lines.

AbsBase1 has an abstract method called doThis(). ConcreteBase
implements a virtual doThis(). ConcreteDerived has no doThis() method.
This causes a compiler error saying that ConcreteDerived needs to
implement doThis(). I was surprised it didn't compile, since I figured
that ConcreteBase's doThis() implementation would serve as the
ConcreteDerived implementation. So we want to do something like this:

Does this compiler error seem correct? If so, what do I need to do to
get this design to work? Namely, what do I need to do to be able to
get doThis() to run polymorphically through AbsBase1 given an object of
type ConcreteDerived?

AbsBase1* myAbsBase1 = new ConcreteDerived();
myAbsBase1->doThis();

Thanks for any input!

Ken

Mar 30 '06 #1
5 1458
The following inheritance hierarchy applies (arrows point to base
classes):

AbsBase1<---AbsBase2<---ConcreteDerived

ConcreteBase<---ConcreteDerived

Note that ConcreteDerived is the same class type on both lines.

AbsBase1 has an abstract method called doThis(). ConcreteBase
implements a virtual doThis(). ConcreteDerived has no doThis() method.
This causes a compiler error saying that ConcreteDerived needs to
implement doThis(). I was surprised it didn't compile, since I figured
that ConcreteBase's doThis() implementation would serve as the
ConcreteDerived implementation.


Read this:

http://www.informit.com/content/imag...er/alexch1.pdf

Mar 30 '06 #2
kk****@yahoo.com wrote:
We have AbsBase1, AbsDerived1, AbsBase2, ConcreteBase, and
ConcreteDerived.

The following inheritance hierarchy applies (arrows point to base
classes):

AbsBase1<---AbsBase2<---ConcreteDerived

ConcreteBase<---ConcreteDerived

Note that ConcreteDerived is the same class type on both lines.


Do you mean ConcreteDerived uses multiple inheritance like this:

struct ConcreteDerived : AbsBase2, ConcreteBase { ... };

Perhaps one of these FAQs will answer your question:

http://www.parashift.com/c++-faq-lit...html#faq-25.10

http://www.parashift.com/c++-faq-lit....html#faq-24.3

If not, please give us more details (code?) on the relationship between
the classes.

Cheers! --M

Mar 30 '06 #3
For polimorphism, C++ implementations use vtables.
Virtual methods in different classes in the same hierarchy, occupy the
same slot in vtable.
I think implementation of vtable is compiler-dependant, but usually
would be something like:

vtable for AbsBase1 (pointers or references)

slot 1 virtual method1
slot 2 virtual method2
slot ... your doThis (naturally virtual)
slot n virtual methodn

vtable for AbsBase2:

slots of AbsBase1
slots of AbsBase2 (those that aren't in AbsBase1)

vtable for ConcreteBase:

slot 1 virtual method1
slot 2 virtual method2
slot ... your doThis (naturally virtual)
slot n virtual methodn

vtable for ConcreteDerived:

slots of AbsBase2 (of course includes AbsBase1 slots)
slots of ConcreteBase

thus there're 2 slots for doThis, because there are 2 diferent sections.
One for each hierarchy.
In vtable for type ConcreteDerived, only assign a value to doThis slot
in ConcreteBase vtable section in Concrete derived but not the
corresponding slot in slots proceding from AbsBase1 hierarchy.
When you are using a pointer to AbsBase1, you are using a view of
ConcreteDerived class, corresponding to AbsBase1 section in the vtable,
thus you hasn't doThis.

kk****@yahoo.com wrote:
I'm having an inheritance problem. My mom says she just won't leave me
her Three Stooges DVD collection when she's gone. Okay, just a bit of
OO levity. Here's the real problem:

We have AbsBase1, AbsDerived1, AbsBase2, ConcreteBase, and
ConcreteDerived.

The following inheritance hierarchy applies (arrows point to base
classes):

AbsBase1<---AbsBase2<---ConcreteDerived

ConcreteBase<---ConcreteDerived

Note that ConcreteDerived is the same class type on both lines.

AbsBase1 has an abstract method called doThis(). ConcreteBase
implements a virtual doThis(). ConcreteDerived has no doThis() method.
This causes a compiler error saying that ConcreteDerived needs to
implement doThis(). I was surprised it didn't compile, since I figured
that ConcreteBase's doThis() implementation would serve as the
ConcreteDerived implementation. So we want to do something like this:

Does this compiler error seem correct? If so, what do I need to do to
get this design to work? Namely, what do I need to do to be able to
get doThis() to run polymorphically through AbsBase1 given an object of
type ConcreteDerived?

AbsBase1* myAbsBase1 = new ConcreteDerived();
myAbsBase1->doThis();

Thanks for any input!

Ken

Mar 31 '06 #4
Please don't top-post.

Carlos Martinez wrote:
For polimorphism, C++ implementations use vtables.
That's one way. It's about the only way I've come across. But it's not
mandatory.
Virtual methods in different classes in the same hierarchy, occupy the
same slot in vtable.
I think implementation of vtable is compiler-dependant, but usually
would be something like:


*If* an implementation uses vtables, the way it uses them is indeed
compiler-dependent. So you should be aware that if you take advantage
of the particular way your compiler handles vtable, you are making your
code highly non-portable.

Gavin Deane

Mar 31 '06 #5
Gavin Deane wrote:
Please don't top-post.

Carlos Martinez wrote:
For polimorphism, C++ implementations use vtables.

That's one way. It's about the only way I've come across. But it's not
mandatory.

Virtual methods in different classes in the same hierarchy, occupy the
same slot in vtable.
I think implementation of vtable is compiler-dependant, but usually
would be something like:

*If* an implementation uses vtables, the way it uses them is indeed
compiler-dependent. So you should be aware that if you take advantage
of the particular way your compiler handles vtable, you are making your
code highly non-portable.


Yes, I know is implementation-dependant. What I want is to put an
example for understanding the problem, and why virtual methods with the
same name in differente hierarchies aren't interchangeables.

I thought that explaining that implementation, was a good maner for
understanding why doesn't compilers recognize the method doThis.

I'm not an expert and may be, I'm not clear enough explainig it.

Gavin Deane

Mar 31 '06 #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...
0
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing...
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
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...
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...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.