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;
}
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;
}