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);
}
-
This is pretty close to an MVCE but your anyMethods dont return anything and theres an extra } in Comparable.Borgleader– Borgleader2021-11-23 14:56:31 +00:00Commented Nov 23, 2021 at 14:56
-
Thx, corrected.g.pickardou– g.pickardou2021-11-23 17:14:41 +00:00Commented Nov 23, 2021 at 17:14
Add a comment
|