Can't write member comparator function for sorting | Sololearn: Learn to code for FREE!

+2

Can't write member comparator function for sorting

Hi We can overload < operator for our class and this can be used directly for comparison required for sorting. However, if by any chance, I don't want to overload the < operator, we can have option of writing comparator function. This also works fine if I declare the same function as non member function. Is it must that we have to have comparator as non member function or am I missing something to call member comparator in sort function?

7/3/2020 6:53:02 AM

Ketan Lalcheta

1 Answer

New Answer

+3

It is important that if you want to use < without overload, both the objects should not be a part of that class in which there is overloading of < for some other purpose! If one of them is a part of that class and the other is outside the class, the overloading will still not work! So, in short, overloading will be used only if both the objects are of that class. If even one of them is not a part of it then overloading will not be used. I hope it is clear now!👍