Connecting Tech Pros Worldwide Help | Site Map

Issues with multiple inheritance

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 24th, 2007, 08:15 AM
Alexander Adam
Guest
 
Posts: n/a
Default Issues with multiple inheritance

Hi!

I got a class structure similar to:

class Base_Object {
... some functions ...
}

class Object:
public Base_Object,
public IDispatch
{
.. implementation of some Base_Object methods
.. implementation of some IDispatch methods
}

class A : public virtual Object
{}

class B : public virtual Object
{}

class C : public A, public B
{}


Now the issue is this -- I need to get access to the right pointer of
the base Object class. When doing this on class C, the returned result
is wrong and the vTable of the returned "Object" class does point to
some methods in C which is obviously wrong. I understand the paradigma
of the "this" pointer and multiple inheritance and I know that I could
downcast C to A, then downcast to Object and I'd get the right one.
But I cannot and do not want to do this as my class hierarchy goes
much further / deeper. The this example:

class C : public A, public virtual B
{}

class D : public B, public C
{}

Now class D has several methods returning instances of D and C. Within
D, I do not really know which kind of "C" it is, just that the
returned class is inherited from C. All I want to do then is to get
the correct "Object" class out of the returned "C" class so that I can
access Object's methods correctly without using downcasting a few
times. See, I do not really care which Object instance I get because
the Object's methods are calling one virtual method of Base_Object
class which each of the subclasses implemenents so it doesn't really
matter.

I hope my description got clear so far, I didn't find any kind of idea
or solution to this issue eventhough having seeked the web for quite a
while. The only solution was downcasting but I cannot do this for each
class because I got hundreds of classes.

Regards
Alexander


  #2  
Old March 24th, 2007, 08:25 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Issues with multiple inheritance

* Alexander Adam:
Quote:
Hi!
Hi. See <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old March 24th, 2007, 05:35 PM
Adrian Hawryluk
Guest
 
Posts: n/a
Default Re: Issues with multiple inheritance

Alexander Adam wrote:
Quote:
Hi!
>
I got a class structure similar to:
>
class Base_Object {
... some functions ...
}
>
class Object:
public Base_Object,
public IDispatch
{
.. implementation of some Base_Object methods
.. implementation of some IDispatch methods
}
>
class A : public virtual Object
{}
>
class B : public virtual Object
{}
>
class C : public A, public B
{}
>
>
Now the issue is this -- I need to get access to the right pointer of
the base Object class. When doing this on class C, the returned result
is wrong and the vTable of the returned "Object" class does point to
some methods in C which is obviously wrong.
Obviously? No, it inherited those methods (assuming that they are
virtual). It is correct.
Quote:
I understand the paradigma
of the "this" pointer and multiple inheritance and I know that I could
downcast C to A, then downcast to Object and I'd get the right one.
_Assuming_ that you can get the 'right' object doing what you stated
(I'm not sure if what you are saying is correct, I've not touched
multiple-inheritance in a long while), then write a function to do it
for you.

i.e.

Base_Object& C::getBaseObject()
{
return static_cast<Base_Object&>(static_cast<A&>(*this));
}
Quote:
But I cannot and do not want to do this as my class hierarchy goes
much further / deeper. The this example:
>
class C : public A, public virtual B
{}
>
class D : public B, public C
{}
>
Now class D has several methods returning instances of D and C. Within
D, I do not really know which kind of "C" it is, just that the
returned class is inherited from C.
You would then call getBaseObject().
Quote:
All I want to do then is to get
the correct "Object" class out of the returned "C" class so that I can
access Object's methods correctly without using downcasting a few
times. See, I do not really care which Object instance I get because
the Object's methods are calling one virtual method of Base_Object
class which each of the subclasses implemenents so it doesn't really
matter.
>
I hope my description got clear so far, I didn't find any kind of idea
or solution to this issue eventhough having seeked the web for quite a
while. The only solution was downcasting but I cannot do this for each
class because I got hundreds of classes.
Your description was clear as mud. :) Good luck.


Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.