| non-virtual-bases/non-virtual-1.cc - Illustration of replication of class A. | Lecture 5 - slide 19 : 40 Program 1 |
// Illustration of an ambiguity problem when we
// deal with a replicated base class.
#include <iostream>
#include <string>
using namespace std;
class A {
public:
int a;
A(int a): a(a){}
};
class B : public A {
public:
int b;
B(): b(1), A(5) {}
};
class C : public A {
public:
int c;
C(): c(2), A(7){}
};
class D : public B, public C {
public:
int d;
D(): d(9){} // use default constructors of B and C
};
int f(D &x){
cout << x.a << endl; // error: request for member a is ambiguous
cout << x.B::a << endl; // 5
cout << x.C::a << endl; // 7
cout << x.b << endl; // 1
cout << x.c << endl; // 2
cout << x.d << endl; // 9
}
int main(){
D d;
f(d);
}