I want to created a generic sorted linked list. Thus I have an abstract class Data, with a pure operator< (in order to easily sort the list. I believe it is called a comperator.) Now I have a class Job which derives from Data and implements the operator. The problem rises when I try to create a list of jobs and find out that job is also abstract. It happens because the operator I wrote in Job doesn’t have the exact same signature as in Data –
in data:
virtual bool operator<(const Data& other) const =0;
and in job:
virtual bool operator<(const Job& other) const;
I had to receive job in job’s operator since I can’t compare job to a generic data. But now the new operator just hides the old one instead of overriding it (or implementing it because it is pure).
How can I solve the problem without using downcasting?
Thanks!
Edit: and without templates.
Without using downcasting… seems like a job for templates.
You can enforce the implementation of the comparison function using the operator directly, and not necessarily declaring it as pure
virtual. And it’s also generic.Same result, different approach. More flexibility, since you don’t have to derive from something (which can become a pain if you start using multiple inheritance).