one additional querstion about multiple inheritance

T

Tony Johansson

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
 
K

Karl Heinz Buchegger

Tony said:
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?
 
G

Greger

Tony said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top