For example: in a C++ header file, if I defined a struct Record
and I would like to use it for possible sorting so that I want to overload the less operator
Favor in-class unless it cannot be in-class because first argument is the wrong type.
The best way to define the less operator is:
struct Record{
(...)
const bool operator < ( const Record &r ) const{
return ( num < r.num );
}
};
They are essentially the same, other than the first being non-const and allowing you to modify itself.
I prefer the second for 2 reasons:
friend
.lhs
does not have to be a Record