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);
}
g.pickardou
  • 32,346
  • 36
  • 123
  • 268

0 Answers0