1
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);
}
2
  • This is pretty close to an MVCE but your anyMethods dont return anything and theres an extra } in Comparable. Commented Nov 23, 2021 at 14:56
  • Thx, corrected. Commented Nov 23, 2021 at 17:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.