一、多继承
拥有多个父类的继承,叫做多继承
二义性
例子
class A
{
public:
int a=4;
};
class C
{
public:
int a = 2;
};
class B:public A,public C
{
public:
void s()
{
//会产生二义性
//cout << a <<a<< endl;错误
cout << A::a <<B::a<< endl;
}
};
在多继承中,使用virtual 无法解决二义性问题,只能用作用域分辨符::
二、菱形继承
class GrandFather
{
public:
GrandFather(int rule):m_rule(rule) {
}
};
class Father:virtual public GrandFather
{
public:
Father(int f) :m_f(f),GrandFather(0) {
}
protected:
int m_f;
int m_age = 52;
};
class Mother:virtual public GrandFather
{
public:
Mother(int m = 38) :m_m(m), GrandFather(0){
}
~Mother() {
}
protected:
int m_m;
int m_age = 48;
};
class Child :public Father