45 lines
537 B
C++
45 lines
537 B
C++
// 多继承(同属一个超级父类)产生的问题
|
|
#include <iostream>
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
class A
|
|
{
|
|
public:
|
|
int x;
|
|
};
|
|
|
|
class B : virtual public A
|
|
{
|
|
public:
|
|
B()
|
|
{
|
|
x = 50;
|
|
}
|
|
};
|
|
|
|
class C : virtual public A
|
|
{
|
|
public:
|
|
C()
|
|
{
|
|
x = 100;
|
|
}
|
|
};
|
|
|
|
class D : public C, public B
|
|
{
|
|
};
|
|
|
|
int main()
|
|
{
|
|
D d1;
|
|
cout << d1.x << endl;
|
|
d1.x = 0; // x 是从 B 来的,还是从 C 来的? C ,使用就近原则
|
|
cout << d1.x << endl;
|
|
|
|
return 0;
|
|
}
|