Yes, I know it's bad tone to compare different descendants of abstract class. But I really have to.
I have this simple abstract class:
class Figure {
public:
virtual double Square() = 0;
virtual ~Figure() {};
}
What I want to add to it is something like:
bool operator<(const Figure& other) {};
Which would compare its descendants - geometric figures, namely Triangle, Rectangle, Octagon - using their area returned in method Square().
Is that possible in any way?
From your comment
return (*this->Square() < other->Square());your trouble simply seems to be basic syntax.thisis a simple pointer. So the typical correct syntax is justthis->Square(). Of course since it's inside a member function thenthiscan be omitted entirely as justSquare().otheris a reference, so that uses the dot operator asother.Square()Another useful thing, potentially relevant to your latest comment, is to make the operator< function
constsince it isn't modifying the the object it's being called on.So the resulting code should be something more like: