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

Multiple inheritance and casting

If I have a class hierarchy as follows:

class barBase
{
};

class barDerived:public barBase
{
};

class fooBase
{
};

class fooBarDerived:public fooBase, barDerived
{
};

And if I create an instance as follows:

fooBase* myFooBase = new fooBase;

Is there any way I can then cast myFooBase to a barBase, without
knowing about fooBarDerived?

(this code should only know about barBase and fooBase)

barBase* myBarBase = some_cast(myFooBase);

Intuitively I'd think there ought to be, because myFooBase points to an
object which is, in part, a barBase. But I can't see any way of doing
this.

--
Simon Elliott http://www.ctsn.co.uk
Jul 22 '05 #1
3 1327

"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:41***********************@news.gradwell.net.. .
If I have a class hierarchy as follows:

class barBase
{
};

class barDerived:public barBase
{
};

class fooBase
{
};

class fooBarDerived:public fooBase, barDerived
{
};

And if I create an instance as follows:

fooBase* myFooBase = new fooBase;
I think you mean

fooBase* myFooBase = new fooBarDerived;

Is there any way I can then cast myFooBase to a barBase, without
knowing about fooBarDerived?

(this code should only know about barBase and fooBase)

barBase* myBarBase = some_cast(myFooBase);

Intuitively I'd think there ought to be, because myFooBase points to an
object which is, in part, a barBase. But I can't see any way of doing
this.


Assuming my correction above is right, then

barBase* myBarBase = dynamic_cast<barBase*>(myFooBase);

but this only works if your classes have at least one virtual function.

john
Jul 22 '05 #2
Simon Elliott posted:
If I have a class hierarchy as follows:

class barBase
{
};

class barDerived:public barBase
{
};

class fooBase
{
};

class fooBarDerived:public fooBase, barDerived
{
};

And if I create an instance as follows:

fooBase* myFooBase = new fooBase;

Is there any way I can then cast myFooBase to a barBase, without
knowing about fooBarDerived?
There's no relationship whatsoever between the classes, "fooBase" and
"barBase", so there's nothing you can do. But... if you want to use brute
force:

barBase &barbase_object = *reinterpret_cast<barBase* const>
(&foobase_object);

Intuitively I'd think there ought to be, because myFooBase points to an
object which is, in part, a barBase.


Incorrect.

An object of the class "fooBarDerived" would contain both a "fooBase" object
and a "barBase" object.

fooBarDerived fb_derived_object;

barBase& blah1 = fb_derived_object;

fooBase& blah2 = fb_derived_object;

and also:

barDerived& blah3 = fb_derived_object;
-JKop
Jul 22 '05 #3
On 01/10/2004, John Harrison wrote:
And if I create an instance as follows:

fooBase* myFooBase = new fooBase;
I think you mean

fooBase* myFooBase = new fooBarDerived;


Well spotted!
Assuming my correction above is right, then

barBase* myBarBase = dynamic_cast<barBase*>(myFooBase);

but this only works if your classes have at least one virtual
function.


Thanks! I hadn't realised that dynamic_cast could work in cases where
the whole class hierarchy was not visible.

--
Simon Elliott http://www.ctsn.co.uk
Jul 22 '05 #4

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

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
6
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
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...
1
by: vinoraja | last post by:
There are a number of reasons we don't implement Multiple Implementation Inheritance directly. (As you know, we support Multiple Interface Inheritance). However, I should point out that it's...
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...
7
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...
47
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...

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.