468,765 Members | 801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

question and understanding about multiple inheritance

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;
}
Aug 17 '05 #1
5 1559
Tony Johansson wrote:
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

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

Tony Johansson wrote:
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

Aug 17 '05 #3
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....

Aug 17 '05 #4
> 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

Aug 17 '05 #5

Tony Johansson wrote:
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;
}
#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;
}


Aug 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Morgan Cheng | last post: by
2 posts views Thread by Tony Johansson | last post: by
22 posts views Thread by Matthew Louden | last post: by
13 posts views Thread by John Salerno | last post: by
3 posts views Thread by Quentin Huo | last post: by
2 posts views Thread by et | last post: by
7 posts views Thread by tshad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.