class Fruit {
public:
virtual int anyMethod() {
return 0;
}
};
class Comparable {
public:
virtual int compareTo(Comparable *other) = 0;
};
class Apple : public Fruit, public Comparable {
int m_state;
public:
Apple(int state) {
this->m_state = state;
}
int compareTo(Comparable *other) {
return this->m_state - ((Apple *)other)->m_state;
}
int anyMethod() {
return 1;
}
};
void sort(Comparable *c[]) {
// POC: The Apple'a compareTo implementation shoulb called
// but instead Apple's anyMethos is called.
if (c[0]->compareTo(c[1])) {
}
}
int main(void) {
Apple a1(21), a2(10), a3(30);
Apple* apples[3] = {&a1,&a2, &a3};
sort((Comparable **) apples);
}
Asked
Active
Viewed 49 times
1

g.pickardou
- 32,346
- 36
- 123
- 268
-
This is pretty close to an MVCE *but* your anyMethods dont return anything and theres an extra } in Comparable. – Borgleader Nov 23 '21 at 14:56
-
Thx, corrected. – g.pickardou Nov 23 '21 at 17:14