473,406 Members | 2,217 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,406 software developers and data experts.

one additional querstion about multiple inheritance

Hello Experts!

I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand
completely.

When I have these two lines in main I get this compile error of ambigious
vector<MBase*> b;
b.push_back(=new MI);

The reason for this according to the book is the following text.
"In the array definition for b[] this code attempts to create a new MI and
upcast
the adres to a MBase*. The compiler won't accept this because it has no way
of
knowing whether you want to use D1's subobject MBase or D2's subobject MBase
for the resulting address"

As far as I understand I don't use any subobject of D1 or D2 I only use an
array of pointers of the
static type MBase that can point to object of class MBase or objects derived
from this MBase class according to the substitution rule.

I understand this part of the text above
"In the array definition for b[] this code attempts to create a new MI and
upcast
the adres to a MBase*.
#include <iostream>
using namespace std;
#include <vector>

class MBase
{
public:
virtual char* vf() const = 0;
virtual ~MBase() {}
private:
int number;
};

class D1 : public MBase
{
public:
char* vf() const {return "D1"; }
};

class D2 : public MBase
{
public:
char* vf() const {return "D2";}
};

class MI : public D1, public D2
{
};

include "MBase.h"
int main()
{
vector<MBase*> b;
b.push_back(=new MI);
return 0;
}

Many thnaks

//Tony
Aug 17 '05 #1
2 1127
Tony Johansson wrote:

Hello Experts!

I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand
completely.

When I have these two lines in main I get this compile error of ambigious
vector<MBase*> b;
b.push_back(=new MI);


The inheritance hierarchy looks like this

MBase MBase
| |
D1 D2
\ /
\ /
\ /
MI

In other words: each MI object contains *2* MBase objects.
One due to D1 and the second due to D2. Those MBase objects
haven't anything in common, they are distinct objects.

Now, if you force the compiler to take a pointer to MBase, which
one should it choose?

--
Karl Heinz Buchegger
kb******@gascad.at
Aug 17 '05 #2
Tony Johansson wrote:
Hello Experts!

I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand
completely.

When I have these two lines in main I get this compile error of ambigious
vector<MBase*> b;
b.push_back(=new MI);

The reason for this according to the book is the following text.
"In the array definition for b[] this code attempts to create a new MI and
upcast
the adres to a MBase*. The compiler won't accept this because it has no
way of
knowing whether you want to use D1's subobject MBase or D2's subobject
MBase for the resulting address"

As far as I understand I don't use any subobject of D1 or D2 I only use
an array of pointers of the
static type MBase that can point to object of class MBase or objects
derived from this MBase class according to the substitution rule.

I understand this part of the text above
"In the array definition for b[] this code attempts to create a new MI and
upcast
the adres to a MBase*.
#include <iostream>
using namespace std;
#include <vector>

class MBase
{
public:
virtual char* vf() const = 0;
virtual ~MBase() {}
private:
int number;
};
try:
class D1 : public virtual MBase {
public:
char* vf() const {return "D1"; }
};
and try:

class D2 : public virtual MBase {
public:
char* vf() const {return "D2";}
};

class MI : public D1, public D2
{
};

include "MBase.h"
int main()
{
vector<MBase*> b;
b.push_back(=new MI);
return 0;
}

Many thnaks

//Tony


--
http://www.gregerhaga.net
Aug 17 '05 #3

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?
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...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
20
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...
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...
2
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). --...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...

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.