理想的菱形继承如图所示
#include "iostream"using namespace std;class top
{public:void baseshow(){cout << "baseshow" << endl;}
};class left:public::top
{public:void leftshow(){cout << "leftshow" << endl;}
};class right:public::top
{public:void rightshow(){cout << "rightshow" << endl;}
};class bottom:public left,public right
{public:void bottomshow(){}
};int main()
{bottom rlxy;rlxy.bottomshow();
}
实际上的类型继承如图所示
通过查看代码,发现报错信息为基类有歧义
解决二义性的问题(治根不治本)
我们声明路径即可
解决二义性的问题(完美解决)
使用虚继承即可
#include "iostream"using namespace std;class top
{public:void baseshow(){cout << "baseshow" << endl;}
};class left:virtual public::top
{public:void leftshow(){cout << "leftshow" << endl;}
};class right:virtual public::top
{public:void rightshow(){cout << "rightshow" << endl;}
};class bottom:public left,public right
{public:
};int main()
{bottom rlxy;rlxy.baseshow();
}