多继承中的二义性问题
using namespace std;class Base
{
public:
Base(int i):b(i) {cout<<"Base constructor. b="<<i<<endl;}
~Base(){cout<<"Base destructor. b="<<b<<endl;}
int GetB(){return b;}
private:
int b;
};
class Base11:virtual public Base
{
public:
Base11(int i,int j):Base(i),b11(j)
{
cout<<"Base11 constructor."<<endl;
}
~Base11(){cout<<"Base11 destructor."<<endl;}
void Print()
{
cout<<"Data in Base11. b11="<<b11<<",b="<<GetB()<<endl;
}
private:
int b11;
};
class Base12:virtual public Base
{
public:
Base12 (int i,int j):Base(i),b12(j)
{
cout<<"Base12 constructor.";
}
~Base12(){cout<<"Base12 destructor."<<endl;}
void Print()
{
cout<<"Data in Base12. b12="<<b12<<",b="<<GetB()<<endl;
}
private:
int b12;
};
class Base2:public Base
{
public :
Base2(int i,int j):Base(i),b2(j)
{
cout<<"Base2 constructor."<<endl;
}
~Base2() {cout<<"Base2 destructor."<<endl;}
void Print()
{
cout<<"Data in Base2. b2="<<b2<<",b="<<GetB()<<endl;
}
private:
int b2;
};
class Derived:public Base11,public Base12,public Base2
{
public:
Derived(int i,int j,int k,int l,int m,int n,int x,int y):Base11(i,j),Base12(k,l),Base2(m,n),Base(x),d(y)
{
cout<<"Derived constructor."<<endl;
}
~Derived(){cout<<"Derived destructor."<<endl;}
int GetD(){return d;}
private:
int d;
};
int main()
{
Derived obj(1,2,3,4,5,6,7,8);
obj.Base11::Print();
obj.Base12::Print();
obj.Base2::Print();
cout<<"Data in itself. d="<<obj.GetD()<<endl;
return 0;
}
这个程序是我教材里的一个例子,但是我编译时报错,出现了二义性,想了一下,的却是的,而书本却能给出了下面的输出结果,想问是怎么回事?
Base constructor. b=7
Base11 constructor.
Base12 constructor.
Base constructor. b=5
Base2 constructor.
Derived constructor.
Data in Base11. b11=2,b=7
Data in Base12. b12=4,b=7
Data in Base2. b2=6,b=5
Data in itself. d=8
...