I have two set of code :
First case :
#include<iostream>
using namespace std;
class A {
public:
virtual void display() { cout << "A display \n"; }
};
class B : virtual public A {
public:
void display() { cout << "B display \n"; }
};
class C : virtual public A {
public:
void display() { cout << "C display \n"; }
};
class D : public C, public B {
public:
D() {};
};
int main() {
D d;
d.B::display(); // ERROR
}
Error :
Error (active) E0318 override of virtual function "A::display" is ambiguous
Error C2250 'D': ambiguous inheritance of 'void A::display(void)'
Second case :
#include<iostream>
using namespace std;
class A {
public:
void display() { cout << "A display \n"; }
};
class B : virtual public A {
public:
void display() { cout << "B display \n"; }
};
class C : virtual public A {
public:
void display() { cout << "C display \n"; }
};
class D : public C, public B {
public:
D() {};
};
int main() {
D d;
d.B::display(); // Output B display
}
The only difference in above two cases is in first one I am making "virtual void display()" in class A, why it is failing in first case while passing in second case?
Aucun commentaire:
Enregistrer un commentaire