Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 17th, 2005, 10:55 AM
Tony Johansson
Guest
 
Posts: n/a
Default 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


  #2  
Old August 17th, 2005, 11:15 AM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: one additional querstion about multiple inheritance

Tony Johansson wrote:[color=blue]
>
> 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);
>[/color]

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
kbuchegg@gascad.at
  #3  
Old August 17th, 2005, 12:35 PM
Greger
Guest
 
Posts: n/a
Default Re: one additional querstion about multiple inheritance

Tony Johansson wrote:
[color=blue]
> 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;
> };
>[/color]
try:
class D1 : public virtual MBase[color=blue]
> {
> public:
> char* vf() const {return "D1"; }
> };
>[/color]
and try:

class D2 : public virtual MBase[color=blue]
> {
> 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[/color]

--
http://www.gregerhaga.net
 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles