473,624 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 24 '07 #1
2 2027
* Alexander Adam:
Hi!
Hi. See <url:
http://www.parashift.c om/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?
Mar 24 '07 #2
Alexander Adam wrote:
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.
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::getBaseObjec t()
{
return static_cast<Bas e_Object&>(stat ic_cast<A&>(*th is));
}
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() .
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.blogspo t.com/]______\/
Mar 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4327
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
2174
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 multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
30
2710
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
20
10070
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
60
4903
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
15
28342
by: iKiLL | last post by:
hi all, I would like to be able to create an umbrella class for all my main global sections but I would still like to keep them all in separate file something like the below but I keep getting an error saying you are not allowed Multiple base classes. /// <summary>
7
3729
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
47
4003
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
2
2558
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). -- Paul for cls in (A,B,C,D): seen = set()
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.