菱形继承?
class A
{
public:
int a;
int funa()
{
cout << "A::funa()" << endl;
}
};
class B :public A
{
public:
int b;
int funb()
{
cout << "B::funb()" << endl;
}
};
class C :public A
{
public:
int c;
int func()
{
cout << "C::func()" << endl;
}
};
class D :public B, public C
{
public:
int d;
int fund()
{
cout << "D::fund()" << endl;
}
};
int main()
{
D dd;
cout << dd.a << endl;//报错,因为编译器不知道继承的是哪个A类,解决方法:虚基类
return 0;
}
class A
{
public:
int a;
int funa()
{
cout << "A::funa()" << endl;
}
};
class B :virtual public A
{
public:
int b;
int funb()
{
cout << "B::funb()" << endl;
}
};
class C :virtual public A
{
public:
int c;
int func()
{
cout << "C::func()" << endl;
}
};
class D :public B, public C
{
public:
int d;
int fund()
{
cout << "D::fund()" << endl;
}
};
int main()
{
D dd;
cout << dd.a << endl;//正确
return 0;
}
class A
{
public:
A(int a)
{
_a = a;
}
int _a;
int funa()
{
cout << "A::funa()" << endl;
}
};
class B :virtual public A
{
public:
B(int a, int b):A(a)
{
_b = b;
}
int _b;
int funb()
{
cout << "B::funb()" << endl;
}
};
class C :virtual public A
{
public:
C(int a, int c) :A(a)
{
_c = c;
}
int _c;
int func()
{
cout << "C::func()" << endl;
}
};
class D :public B, public C
{
public:
D(int a1, int a2, int b, int c, int d) :A(10), B(a1, b), C(a2, c)
{
_d = d;
}
int _d;
int fund()
{
cout << "D::fund()" << endl;
}
};
int main()
{
D dd(1, 2, 3, 4, 5);
cout << dd._a << endl;//结果为10,D类中的a1,a2的构造不起作用,
//但是又可以删掉,因为删掉之后就找不到对应的符号了
return 0;
}
菱形继承的引入
虚基类的逻辑
- 被继承的类会变成虚基类,虚基类在子对象中存放在vbtable中,原本应该存储该父类对象的位置替换为vbptr,vbptr指向vbtable中虚基类实例的位置,从而保证,虚基类在子类中只会有一个实例的存在
- 注意:虚基类在子类构造时会被当成直接父类进行构造
菱形继承的解决
抽象类
class A
{
public:
virtual void fun() = 0;//纯虚函数,含有纯虚函数的类叫做抽象类
};
/*
class A
{
public:
virtual void fun() = 0;
};
class AA :public A
{
public:
void fun1()
{
cout << "fun1()" << endl;
}
virtual void fun()
{
cout << "fun()" << endl;//将A中的纯虚函数覆盖,所以AA不是抽象类了
}
};
int main()
{
A a;//错误,抽象类不能实例化对象
AA aa;//正确
return 0;
}
抽象类的作用
抽象类的应用
|