question and understanding about multiple inheritance

T

Tony Johansson

Hello Experts!

I just play around just to try to understand this about multiple
inheritance.
You have all the class definition below and at the bottom you have the main
program.

So here I use multiple inheritance.
One top class called MBase and below this we have D1 and D2 on the same
level.
Class MI is at the bottom inheritating from both D1 and D2.

Now to my questions.
My first question is when we instansiate an object of class MI we will get
a subobject of class D1 and one subobject of class D2 within this class MI.
This would result of ambigious for method vf() and the datamember number in
class MBase.

In MI we would get two definition of vf one from D1 and one from D2 which
would cause compile error.

If I in main program add a call to mi.vf() then I get this ambigius compile
error.
But I imagine that this compile error problem should appear when I create an
instance of MI without having this call to mi.vf();


#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()
{
MI mi;
mi.vf(); // this cause compile error ambigious

//vector<MBase*> b;
//b.push_back(new D1);
//b.push_back(new D2);
//b.push_back(new MI);
return 0;
}
 
G

Greg

Tony said:
Hello Experts!

I just play around just to try to understand this about multiple
inheritance.
You have all the class definition below and at the bottom you have the main
program.

So here I use multiple inheritance.
One top class called MBase and below this we have D1 and D2 on the same
level.
Class MI is at the bottom inheritating from both D1 and D2.

Now to my questions.
My first question is when we instansiate an object of class MI we will get
a subobject of class D1 and one subobject of class D2 within this class MI.
This would result of ambigious for method vf() and the datamember number in
class MBase.

In MI we would get two definition of vf one from D1 and one from D2 which
would cause compile error.

If I in main program add a call to mi.vf() then I get this ambigius compile
error.
But I imagine that this compile error problem should appear when I create an
instance of MI without having this call to mi.vf();


#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()
{
MI mi;
mi.vf(); // this cause compile error ambigious

//vector<MBase*> b;
//b.push_back(new D1);
//b.push_back(new D2);
//b.push_back(new MI);
return 0;
}

I'm sorry, but was there a question that I missed? If the question was
how to call vf() when two inherited versions exist, the answer is to
qualify the routine with its class's name:

mi.D1::vf();

Not pretty that's for certain, but it works.

Greg
 
K

kolari

This is called as dimond problem in C++ . For the solution you make
Mbase as virtual base class. This must me sepcified at the time of
inheriting to D1 and D2 .

One more this is not multiple inheritance it is called as hybride
Inheritence .
where Mbase ->D1 and D2 is called hirarcy and
D1 and D2 -> MI is multiple inheritence .
as a hole it is called as hirarchy...

it is some thing like this.
class Mbase {
..........
}
class D1:public virtual Mbase {
..........
}
class D2:public virtual Mbase{
..........
}
class MI: public D1, public D2 {
.........
}
......
I think it will serve your needs....
 
S

Srini

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
{

};

*Always* use virtual inheritance from the most base class in case of
diamond inheritance heirarchies. In this case, virtual inheritance
won't solve the ambiguous call to function 'vf'.

Soln 1. Provide an implementation for function 'vf' in class MI

Soln 2. If you want to use the implementation of one of the base
classes, call it explicitly from the function in MI

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

};

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

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

class MI : public D1, public D2
{
public:
char* vf() const { return D2::vf(); } // explicit call
};

Srini
 
R

Robben

Tony said:
Hello Experts!

I just play around just to try to understand this about multiple
inheritance.
You have all the class definition below and at the bottom you have the main
program.

So here I use multiple inheritance.
One top class called MBase and below this we have D1 and D2 on the same
level.
Class MI is at the bottom inheritating from both D1 and D2.

Now to my questions.
My first question is when we instansiate an object of class MI we will get
a subobject of class D1 and one subobject of class D2 within this class MI.
This would result of ambigious for method vf() and the datamember number in
class MBase.

In MI we would get two definition of vf one from D1 and one from D2 which
would cause compile error.

If I in main program add a call to mi.vf() then I get this ambigius compile
error.
But I imagine that this compile error problem should appear when I create an
instance of MI without having this call to mi.vf();
Ambiguity arises only when the method is invoked, compiler may not be
so smart to point out the ambiguity even before the method is invoked.
Sometimes we may reolve ambiguity by doing type_cast in that case It
would not be wise for the compiler to complain by just checking the
declaration.

Example: In the below class we can solve the ambiguity by doing
type_cast.

class MBase
{
public:
void add(int a) {}
void add(long a) {}
private:
int number;
};


int main()
{
MBase a;
//a.add(10); //ambiguity
a.add((int)10); //no ambiguity
a.add((long)30); //no ambiguity
return 0;
}
 
G

Greger

Tony said:
Hello Experts!

I just play around just to try to understand this about multiple
inheritance.
You have all the class definition below and at the bottom you have the
main program.

So here I use multiple inheritance.
One top class called MBase and below this we have D1 and D2 on the same
level.
Class MI is at the bottom inheritating from both D1 and D2.

Now to my questions.
My first question is when we instansiate an object of class MI we will get
a subobject of class D1 and one subobject of class D2 within this class
MI. This would result of ambigious for method vf() and the datamember
number in class MBase.

In MI we would get two definition of vf one from D1 and one from D2 which
would cause compile error.

If I in main program add a call to mi.vf() then I get this ambigius
compile error.
But I imagine that this compile error problem should appear when I create
an instance of MI without having this call to mi.vf();


#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()
{
MI mi;
mi.vf(); // this cause compile error ambigious

//vector<MBase*> b;
//b.push_back(new D1);
//b.push_back(new D2);
//b.push_back(new MI);
return 0;
}
try making MBase virtual
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top